-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathmain.go
74 lines (59 loc) · 1.48 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
package main
import (
"html/template"
"log"
"net/http"
"strings"
)
func init() {
Config()
MustConnectMongo()
InitDB()
}
func main() {
http.HandleFunc("/", index)
http.HandleFunc("/new", insert)
http.HandleFunc("/drop", drop)
log.Println("Start listening...")
err := http.ListenAndServe(":80", nil)
if err != nil {
panic(err)
}
}
func index(res http.ResponseWriter, req *http.Request) {
defer func() {
if e := recover(); e != nil {
log.Println(e)
res.WriteHeader(http.StatusInternalServerError)
}
}()
log.Println("Index home")
t, err := template.New("foo").Parse(string(tpl))
if err != nil {
log.Println(err)
res.WriteHeader(500)
return
}
data := make(map[string]interface{})
data["List"] = List()
t.Execute(res, data)
}
func insert(res http.ResponseWriter, req *http.Request) {
defer func() {
if e := recover(); e != nil {
log.Println(e)
res.WriteHeader(http.StatusInternalServerError)
}
}()
person := &Person{}
person.Name = strings.Trim(req.FormValue("name"), " ")
person.Phone = strings.Trim(req.FormValue("phone"), " ")
Insert(person)
log.Println("Insert new person %v", *person)
http.Redirect(res, req, "/", 302)
}
func drop(res http.ResponseWriter, req *http.Request) {
log.Println("drop collection")
Drop()
http.Redirect(res, req, "/", 302)
}