forked from eldeal/skills
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
109 lines (88 loc) · 3.21 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"context"
"errors"
dpHealthCheck "github.com/ONSdigital/dp-healthcheck/healthcheck"
dpMongoDB "github.com/ONSdigital/dp-mongodb/health"
"github.com/ONSdigital/log.go/log"
"github.com/cadmiumcat/books-api/api"
"github.com/cadmiumcat/books-api/config"
"github.com/cadmiumcat/books-api/initialiser"
"github.com/cadmiumcat/books-api/mongo"
"github.com/cadmiumcat/books-api/pagination"
"github.com/gorilla/mux"
"os"
)
const serviceName = "books-api"
var (
// BuildTime represents the time in which the service was built
BuildTime string
// GitCommit represents the commit (SHA-1) hash of the service that is running
GitCommit string
// Version represents the version of the service that is running
Version string
// ErrRegisterHealthCheck represents an error when registering a health checker to the healthcheck
ErrRegisterHealthCheck = errors.New("error registering checkers for healthcheck")
)
func main() {
ctx := context.Background()
log.Namespace = serviceName
// Get Config
cfg, err := config.Get()
if err != nil {
log.Event(ctx, "error retrieving the configuration", log.FATAL, log.Error(err))
os.Exit(1)
}
log.Event(ctx, "loaded configuration", log.INFO, log.Data{"config": cfg})
versionInfo, err := dpHealthCheck.NewVersionInfo(BuildTime, GitCommit, Version)
if err != nil {
log.Event(ctx, "could not instantiate health check", log.FATAL, log.Error(err))
os.Exit(1)
}
hc := dpHealthCheck.New(versionInfo, cfg.HealthCheckCriticalTimeout, cfg.HealthCheckInterval)
// Initialise database
mongodb := &mongo.Mongo{}
err = mongodb.Init(cfg.MongoConfig)
if err != nil {
log.Event(ctx, "failed to initialise mongo", log.FATAL, log.Error(err))
os.Exit(1)
}
databaseCollectionBuilder := make(map[dpMongoDB.Database][]dpMongoDB.Collection)
databaseCollectionBuilder[(dpMongoDB.Database)(mongodb.Database)] =
[]dpMongoDB.Collection{
(dpMongoDB.Collection)(mongodb.BooksCollection),
(dpMongoDB.Collection)(mongodb.ReviewsCollection),
}
mongoClient := dpMongoDB.NewClientWithCollections(mongodb.Session.Copy(), databaseCollectionBuilder)
// Add API checks
if err := registerCheckers(ctx, &hc, mongoClient); err != nil {
log.Event(ctx, err.Error(), log.FATAL, log.Error(err))
os.Exit(1)
}
hc.Start(ctx)
// Initialise server
svc := initialiser.Service{}
router := mux.NewRouter()
svc.Server = initialiser.GetHTTPServer(cfg.BindAddr, router)
paginator := pagination.NewPaginator(cfg.DefaultLimit, cfg.DefaultOffset, cfg.DefaultMaximumLimit)
svc.API = api.Setup(ctx, cfg.BindAddr, router, paginator, mongodb, &hc)
svc.Server.ListenAndServe()
hc.Stop()
}
// registerCheckers adds the checkers for the provided clients to the health check object
func registerCheckers(ctx context.Context, hc *dpHealthCheck.HealthCheck, mongoClient *dpMongoDB.Client) error {
var hasErrors bool
mongoHealth := dpMongoDB.CheckMongoClient{
Client: *mongoClient,
Healthcheck: mongoClient.Healthcheck,
}
if err := hc.AddCheck("mongoDB", mongoHealth.Checker); err != nil {
hasErrors = true
log.Event(ctx, "error adding mongoDB checker", log.FATAL, log.Error(err))
}
if hasErrors {
log.Event(ctx, ErrRegisterHealthCheck.Error(), log.ERROR)
return ErrRegisterHealthCheck
}
return nil
}