-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
65 lines (50 loc) · 1.49 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"context"
"log"
"net/http"
controller "github.com/jasonzguo/vaccination-progress-service/controllers"
middleware "github.com/jasonzguo/vaccination-progress-service/middlewares"
repo "github.com/jasonzguo/vaccination-progress-service/repos"
"github.com/julienschmidt/httprouter"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.uber.org/zap"
)
func initializeMongoClient() (*mongo.Client, context.Context) {
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
ctx := context.TODO()
client, err := mongo.Connect(ctx, clientOptions)
if err != nil {
log.Fatal(err)
}
err = client.Ping(ctx, nil)
if err != nil {
log.Fatal(err)
}
return client, ctx
}
func initializeLogger() (*zap.Logger, func()) {
logger := zap.NewExample()
undo := zap.ReplaceGlobals(logger)
return logger, undo
}
func initializeRepo(client *mongo.Client) {
database := client.Database("vaccination")
progressionCollection := database.Collection("progression")
repo.GetProgressionRepo().SetCollection(progressionCollection)
}
func main() {
logger, undo := initializeLogger()
defer logger.Sync()
defer undo()
client, ctx := initializeMongoClient()
defer client.Disconnect(ctx)
initializeRepo(client)
ms := middleware.NewStack()
ms.Use(middleware.Log)
ms.Use(middleware.Authenticate)
router := httprouter.New()
router.GET("/", ms.Wrap(controller.GetProgressionController().FindAll))
log.Fatal(http.ListenAndServe(":8080", router))
}