-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
39 lines (28 loc) · 778 Bytes
/
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
package main
import (
"fmt"
"go-mongo-crud/controllers"
"net/http"
"github.com/julienschmidt/httprouter"
"gopkg.in/mgo.v2"
)
func main() {
router := httprouter.New();
userController := controllers.NewUserController(getSession())
// Routes
router.GET("/users/:id", userController.GetUser)
router.POST("/users", userController.CreateUser)
router.PUT("/users/:id", userController.UpdateUser)
router.DELETE("/users/:id", userController.DeleteUser)
// Start the server
http.ListenAndServe("localhost:8080", router)
}
// Connect returns a MongoDB session
func getSession() *mgo.Session {
session, err := mgo.Dial("mongodb://localhost:27017")
if err != nil {
fmt.Println("Error connecting to MongoDB");
panic(err)
}
return session;
}