-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
89 lines (76 loc) · 1.93 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
package main
import (
"context"
"os"
"os/signal"
"strings"
"sync"
"syscall"
log "github.com/Sirupsen/logrus"
"github.com/adrollxid/bet1/storage/cache/redis"
"github.com/adrollxid/bet1/storage/sql/postgres"
"github.com/akhripko/dummy/healthcheck"
"github.com/akhripko/dummy/metrics"
"github.com/akhripko/dummy/options"
"github.com/akhripko/dummy/prometheus"
"github.com/akhripko/dummy/service"
)
func main() {
// read service config from os env
config := options.ReadEnv()
// init logger
initLogger(config)
log.Info("begin...")
// register metrics
metrics.Register()
// prepare main context
ctx, cancel := context.WithCancel(context.Background())
setupGracefulShutdown(cancel)
var wg = &sync.WaitGroup{}
// build db
db, err := postgres.New(ctx, postgres.Config(config.SQLDB))
if err != nil {
log.Error("sql db init error:", err.Error())
os.Exit(1)
}
// build cache
ccl, err := redis.New(ctx, config.CacheAddr)
if err != nil {
log.Error("cache init error:", err.Error())
os.Exit(1)
}
// build main service
srv := service.New(config.Port, db, ccl)
// build prometheus service
prometheusSrv := prometheus.New(config.PrometheusPort)
// build healthcheck service
healthSrv := healthcheck.New(config.HealthCheckPort, srv.HealthCheck, prometheusSrv.HealthCheck)
// run service
healthSrv.Run(ctx, wg)
prometheusSrv.Run(ctx, wg)
srv.Run(ctx, wg)
// wait while services work
wg.Wait()
log.Info("end")
}
func initLogger(config *options.Config) {
log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(os.Stderr)
switch strings.ToLower(config.LogLevel) {
case "error":
log.SetLevel(log.ErrorLevel)
case "info":
log.SetLevel(log.InfoLevel)
default:
log.SetLevel(log.DebugLevel)
}
}
func setupGracefulShutdown(stop func()) {
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM)
go func() {
<-signalChannel
log.Error("Got Interrupt signal")
stop()
}()
}