forked from zeebo/gostbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
109 lines (89 loc) · 2.74 KB
/
handlers.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
package main
import (
"errors"
"labix.org/v2/mgo/bson"
"net/http"
)
func hello(w http.ResponseWriter, req *http.Request, ctx *Context) (err error) {
//set up the collection and query
coll := ctx.C("entries")
query := coll.Find(nil).Sort("-timestamp")
//execute the query
//TODO: add pagination :)
var entries []Entry
if err = query.All(&entries); err != nil {
return
}
//execute the template
return T("index.html").Execute(w, map[string]interface{}{
"entries": entries,
"ctx": ctx,
})
}
func sign(w http.ResponseWriter, req *http.Request, ctx *Context) (err error) {
//we need a user to sign to
if ctx.User == nil {
err = errors.New("Can't sign without being logged in")
return
}
entry := NewEntry()
entry.Name = ctx.User.Username
entry.Message = req.FormValue("message")
if entry.Message == "" {
entry.Message = "Some dummy who forgot a message."
}
coll := ctx.C("entries")
if err = coll.Insert(entry); err != nil {
return
}
//ignore errors: it's ok if the post count is wrong. we can always look at
//the entries table to fix.
ctx.C("users").Update(bson.M{"_id": ctx.User.ID}, bson.M{
"$inc": bson.M{"posts": 1},
})
http.Redirect(w, req, reverse("index"), http.StatusSeeOther)
return
}
func loginForm(w http.ResponseWriter, req *http.Request, ctx *Context) (err error) {
return T("login.html").Execute(w, map[string]interface{}{
"ctx": ctx,
})
}
func login(w http.ResponseWriter, req *http.Request, ctx *Context) error {
username, password := req.FormValue("username"), req.FormValue("password")
user, e := Login(ctx, username, password)
if e != nil {
ctx.Session.AddFlash("Invalid Username/Password")
return loginForm(w, req, ctx)
}
//store the user id in the values and redirect to index
ctx.Session.Values["user"] = user.ID
http.Redirect(w, req, reverse("index"), http.StatusSeeOther)
return nil
}
func logout(w http.ResponseWriter, req *http.Request, ctx *Context) error {
delete(ctx.Session.Values, "user")
http.Redirect(w, req, reverse("index"), http.StatusSeeOther)
return nil
}
func registerForm(w http.ResponseWriter, req *http.Request, ctx *Context) (err error) {
return T("register.html").Execute(w, map[string]interface{}{
"ctx": ctx,
})
}
func register(w http.ResponseWriter, req *http.Request, ctx *Context) error {
username, password := req.FormValue("username"), req.FormValue("password")
u := &User{
Username: username,
ID: bson.NewObjectId(),
}
u.SetPassword(password)
if err := ctx.C("users").Insert(u); err != nil {
ctx.Session.AddFlash("Problem registering user.")
return registerForm(w, req, ctx)
}
//store the user id in the values and redirect to index
ctx.Session.Values["user"] = u.ID
http.Redirect(w, req, reverse("index"), http.StatusSeeOther)
return nil
}