/
httprouter.go
41 lines (36 loc) · 1.04 KB
/
httprouter.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
package main
import (
"fmt"
"net/http"
"os"
"assignment2/controllers"
// Third party packages
"gopkg.in/mgo.v2"
"github.com/julienschmidt/httprouter"
)
func getSession() *mgo.Session {
// Connect to our local mongo
// s, err := mgo.Dial("mongodb://localhost")
// Connect to our remote mongo
s, err := mgo.Dial("mongodb://admin:123@ds041404.mongolab.com:41404/cmpe273")
// Check if connection error, is mongo running?
if err != nil {
fmt.Println("Can't connect to mongo, go error %v\n", err)
os.Exit(1)
}
return s
}
func main() {
mux := httprouter.New()
// Get a UserController instance
uc := controllers.NewUserController(getSession())
mux.GET("/locations/:id", uc.GetLocations)
mux.POST("/locations/", uc.CreateLocations)
mux.DELETE("/locations/:id", uc.RemoveLocations)
mux.PUT("/locations/:id", uc.UpdateLocations)
server := http.Server{
Addr: "0.0.0.0:8080",
Handler: mux,
}
server.ListenAndServe()
}