forked from swhite24/go-rest-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
45 lines (34 loc) · 907 Bytes
/
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
package main
import (
// Standard library packages
"net/http"
// Third party packages
"github.com/julienschmidt/httprouter"
"github.com/swhite24/go-rest-tutorial/controllers"
"gopkg.in/mgo.v2"
)
func main() {
// Instantiate a new router
r := httprouter.New()
// Get a UserController instance
uc := controllers.NewUserController(getSession())
// Get a user resource
r.GET("/user/:id", uc.GetUser)
// Create a new user
r.POST("/user", uc.CreateUser)
// Remove an existing user
r.DELETE("/user/:id", uc.RemoveUser)
// Fire up the server
http.ListenAndServe("localhost:3000", r)
}
// getSession creates a new mongo session and panics if connection error occurs
func getSession() *mgo.Session {
// Connect to our local mongo
s, err := mgo.Dial("mongodb://localhost")
// Check if connection error, is mongo running?
if err != nil {
panic(err)
}
// Deliver session
return s
}