-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
150 lines (131 loc) · 3.52 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"path"
"regexp"
"strings"
)
// Page data struct for page render
type Page struct {
Title string
Body []byte
}
// ListPageData list of Page
type ListPageData struct {
PageTitle string
Todos []Page
}
const baseTemplatePath = "template/"
const baseNotesPath = "notes/"
func (p *Page) save() error {
filename := baseNotesPath + p.Title + ".txt"
// user read only permission
return ioutil.WriteFile(filename, p.Body, 0600)
}
func loadPage(title string) (*Page, error) {
filename := baseNotesPath + title + ".txt"
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return &Page{Title: title, Body: body}, nil
}
func viewHandler(w http.ResponseWriter, r *http.Request, title string) {
p, err := loadPage(title)
if err != nil {
http.Redirect(w, r, "/edit/"+title, http.StatusFound)
return
}
renderTemplate(w, "view", p)
}
func editHandler(w http.ResponseWriter, r *http.Request, title string) {
p, err := loadPage(title)
if err != nil {
p = &Page{Title: title}
}
renderTemplate(w, "edit", p)
}
func saveHandler(w http.ResponseWriter, r *http.Request, title string) {
body := r.FormValue("body")
p := &Page{Title: title, Body: []byte(body)}
err := p.save()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/view/"+title, http.StatusFound)
}
var templates = template.Must(template.ParseFiles("template/edit.html", "template/view.html"))
func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
err := templates.ExecuteTemplate(w, tmpl+".html", p)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
var validPath = regexp.MustCompile("^/(edit|save|view)/([a-zA-Z0-9]+)$")
func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
m := validPath.FindStringSubmatch(r.URL.Path)
if m == nil {
http.NotFound(w, r)
return
}
fn(w, r, m[2])
}
}
func listHandler(w http.ResponseWriter, r *http.Request) {
files, err := ioutil.ReadDir("./notes")
if err != nil {
log.Fatal(err)
}
pages := []Page{}
for _, file := range files {
fn := file.Name()
p := Page{Title: strings.TrimSuffix(fn, path.Ext(fn))}
pages = append(pages, p)
}
data := ListPageData{
PageTitle: "My notes list",
Todos: pages,
}
tmpl := template.Must(template.ParseFiles("template/list.html"))
err = tmpl.Execute(w, data)
if err != nil {
fmt.Println(err)
}
}
func addHandler(w http.ResponseWriter, r *http.Request) {
// fmt.Println("add method:", r.Method) //get request method
if r.Method == "GET" {
tmpl := template.Must(template.ParseFiles("template/add.html"))
err := tmpl.Execute(w, nil)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
} else {
title := r.FormValue("title")
body := r.FormValue("body")
p := &Page{Title: title, Body: []byte(body)}
err := p.save()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/view/"+title, http.StatusFound)
}
}
func main() {
http.HandleFunc("/view/", makeHandler(viewHandler))
http.HandleFunc("/edit/", makeHandler(editHandler))
http.HandleFunc("/save/", makeHandler(saveHandler))
http.HandleFunc("/", listHandler)
http.HandleFunc("/add", addHandler)
log.Println("The application is running on port 8686...")
log.Fatal(http.ListenAndServe(":8686", nil))
}