-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
52 lines (41 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
package main
import (
"html/template"
"net/http"
)
var index = template.Must(template.ParseFiles(
"templates/_base.html",
"templates/index.html",
))
func hello(w http.ResponseWriter, req *http.Request) {
//grab a clone of the session and close it when the function returns
s := session.Clone()
defer s.Close()
// set up the collection and query
coll := s.DB("gostbook").C("entries")
query := coll.Find(nil).Sort("-timestamp")
// execute the query
// TODO: add pagination
var entries []Entry
if err := query.All(&entries); err := nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// execute the template
if err := index.Execute(w, nil); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
var session *mgo.Session
func main() {
var err error
session, err = mgo.Dial("localhost")
if err != nil {
panic(err)
}
http.HandleFunc("/", hello)
if err := http.ListenAndServe(":8111", nil); err != nil {
panic(err)
}
http.HandleFunc("/sign", sign)
}