forked from zeebo/gostbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (58 loc) · 1.53 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
package main
import (
"code.google.com/p/gorilla/pat"
"code.google.com/p/gorilla/sessions"
"encoding/gob"
"fmt"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"net/http"
"os"
)
func reverse(name string, things ...interface{}) string {
//convert the things to strings
strs := make([]string, len(things))
for i, th := range things {
strs[i] = fmt.Sprint(th)
}
//grab the route
u, err := router.GetRoute(name).URL(strs...)
if err != nil {
panic(err)
}
return u.Path
}
func init() {
gob.Register(bson.ObjectId(""))
}
var store sessions.Store
var session *mgo.Session
var database string
var router *pat.Router
func main() {
var err error
session, err = mgo.Dial(os.Getenv("DATABASE_URL"))
if err != nil {
panic(err)
}
database = session.DB("").Name
//create an index for the username field on the users collection
if err := session.DB("").C("users").EnsureIndex(mgo.Index{
Key: []string{"username"},
Unique: true,
}); err != nil {
panic(err)
}
store = sessions.NewCookieStore([]byte(os.Getenv("KEY")))
router = pat.New()
router.Add("GET", "/login", handler(loginForm)).Name("login")
router.Add("POST", "/login", handler(login))
router.Add("GET", "/register", handler(registerForm)).Name("register")
router.Add("POST", "/register", handler(register))
router.Add("GET", "/logout", handler(logout)).Name("logout")
router.Add("GET", "/", handler(hello)).Name("index")
router.Add("POST", "/sign", handler(sign)).Name("sign")
if err = http.ListenAndServe(":"+os.Getenv("PORT"), router); err != nil {
panic(err)
}
}