-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
56 lines (49 loc) · 1.08 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
package main
import (
"log"
"net/http"
)
var notes map[string]string
func main() {
populateTemplates()
notes = map[string]string{
"tears": "Tears in heaven",
"bad": "Yes I'm everybody's Mr Bad Guy",
}
http.HandleFunc("/note/", noteHandler)
http.Handle("/static/", http.FileServer(http.Dir("public")))
log.Fatalln(http.ListenAndServe(":5000", nil))
}
func noteHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
title := r.URL.Path[len("/note/"):]
if title == "" {
err := noteListTemplate.Execute(w, noteListData{notes})
if err != nil {
log.Fatalln(err)
}
return
}
note, ok := notes[title]
if ok {
err := noteTemplate.Execute(w, noteData{title, note})
if err != nil {
log.Fatalln(err)
}
return
} else {
http.NotFound(w, r)
return
}
}
if r.Method == "POST" {
if r.URL.Path != "/note/" {
http.Error(w, "not found", http.StatusNotFound)
return
}
title := r.FormValue("title")
content := r.FormValue("content")
notes[title] = content
http.Redirect(w, r, "/note/"+title, http.StatusFound)
}
}