-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
42 lines (34 loc) · 942 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
40
41
42
package main
import (
"log"
"net/http"
"os"
"github.com/hdnha11/go-api-example/project"
"github.com/joho/godotenv"
"github.com/julienschmidt/httprouter"
"gopkg.in/mgo.v2"
)
func main() {
godotenv.Load()
// Connect to MongoDB
session, err := mgo.Dial(os.Getenv("MONGODB_HOST"))
if err != nil {
log.Fatal("Cannot connect to MongoDB: ", err)
}
defer session.Close()
db := session.DB(os.Getenv("MONGODB_DATABASE"))
router := httprouter.New()
// Projects
projectRepository := project.NewMongoRepository(db)
projectService := project.NewService(projectRepository)
project.InitRouters(router, projectService)
// Ping
router.GET("/status", func(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
res.WriteHeader(http.StatusOK)
})
port := os.Getenv("PORT")
log.Printf("GoAPIExample is listening on port %s", port)
if err := http.ListenAndServe(":"+port, router); err != nil {
log.Fatal(err)
}
}