forked from tquach/golang-rest-server
/
server.go
84 lines (68 loc) · 1.78 KB
/
server.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
package main
import (
"flag"
"github.com/codegangsta/martini"
"github.com/martini-contrib/binding"
"github.com/martini-contrib/render"
"github.com/tantastik/golang-rest-server/handlers"
"labix.org/v2/mgo"
)
// DATABASE_URL is of the form:
// [mongodb://][user:pass@]host1[:port1][,host2[:port2],...][/database][?options]
var (
databaseUrl = flag.String("databaseUrl", "localhost", "url of database, eg. mongodb://localhost:27017")
databaseName = flag.String("databaseName", "test", "database name, eg. myDatabase")
)
type Server struct {
*martini.Martini
martini.Router
}
// Example of a basic document binding
type Query struct {
MaxResults int `form:"max_results"`
Page int `form:"page"`
Offset int `form:"offset"`
}
// Adds routes to the server
func (s *Server) AddRoutes() {
// Bind to a basic form for passing in additional query parameters
s.Get("/:resource/:id", binding.Bind(Query{}), handlers.GetResource)
s.Get("/:resource", binding.Bind(Query{}), handlers.ListResources)
s.Post("/:resource/", handlers.SaveResource)
}
func AppServer() *Server {
r := martini.NewRouter()
m := martini.New()
m.Use(martini.Logger())
m.Use(martini.Recovery())
m.Use(render.Renderer())
m.Use(DB())
m.Action(r.Handle)
return &Server{m, r}
}
func DB() martini.Handler {
session, err := mgo.Dial(*databaseUrl)
if err != nil {
panic(err)
}
return func(c martini.Context) {
// Clone the session.
s := session.Clone()
// Map a reference to the database to the request context
c.Map(s.DB(*databaseName))
// Clean up our session.
defer s.Close()
// Pass control to next handler
c.Next()
}
}
func main() {
// Parse command line arguments
flag.Parse()
// Init the app server
s := AppServer()
// Add some routes
s.AddRoutes()
// Run all the things
s.Run()
}