-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
64 lines (56 loc) · 1.68 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
package main
import (
"local/Grocy/Controller"
"local/Grocy/Godeps/_workspace/src/github.com/martini-contrib/render"
"local/Grocy/Godeps/_workspace/src/labix.org/v2/mgo"
"local/Grocy/Godeps/_workspace/src/packages/github.com/go-martini/martini"
"os"
)
var martiniApp *martini.Martini
var martiniRouter martini.Router
func main() {
martiniApp = martini.New()
martiniRouter = martini.NewRouter()
var baseController = Controller.GetBaseController()
baseController.HandleRouting(martiniRouter)
//Not quite sure why they are needed
martiniApp.Use(martini.Recovery())
martiniApp.Use(martini.Logger())
martiniApp.Use(render.Renderer())
martiniApp.Use(db())
martiniApp.Action(martiniRouter.Handle)
martiniApp.Run()
}
/*
the function returns a martini.Handler which is called on each request. We simply clone
the session for each request and close it when the request is complete. The call to c.Map
maps an instance of *mgo.Database to the request context. Then *mgo.Database
is injected into each handler function.
*/
func db() martini.Handler {
session, err := mgo.Dial(os.Getenv("MGO_DB_CONNECTION_STRING"))
if err != nil {
panic(err)
}
return func(c martini.Context) {
s := session.Clone()
c.Map(s.DB(os.Getenv("MONGO_DB"))) // local
defer s.Close()
c.Next()
}
}
//
//func UserMapper(c martini.Context, req *http.Request) {
// var token = req.Form.Get("uid")
// jwtToken, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
// return []byte("WauWau123"), nil
// })
//
// if err == nil && jwtToken.Valid {
// var currentUser = Models.CurrentUserModel{jwtToken.Claims["userid"].(string)}
// c.Map(currentUser)
// c.Next()
// } else {
// c.Next()
// }
//}