-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
160 lines (140 loc) · 3.58 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"context"
"flag"
"github.com/configcat/configcat-proxy/config"
"github.com/configcat/configcat-proxy/diag"
"github.com/configcat/configcat-proxy/diag/metrics"
"github.com/configcat/configcat-proxy/diag/status"
"github.com/configcat/configcat-proxy/grpc"
"github.com/configcat/configcat-proxy/log"
"github.com/configcat/configcat-proxy/sdk"
"github.com/configcat/configcat-proxy/sdk/store/cache"
"github.com/configcat/configcat-proxy/web"
"os"
"os/signal"
"sync"
"syscall"
"time"
)
const (
exitOk = iota
exitFailure
)
func main() {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT)
os.Exit(run(sigChan))
}
func run(closeSignal chan os.Signal) int {
logger := log.NewLogger(os.Stderr, os.Stdout, log.Warn)
logger.Reportf("service starting...")
var configFile string
flag.StringVar(&configFile, "c", "", "path to the configuration file")
flag.Parse()
conf, err := config.LoadConfigFromFileAndEnvironment(configFile)
if err != nil {
logger.Errorf("%s", err)
return exitFailure
}
err = conf.Validate()
if err != nil {
logger.Errorf("%s", err)
return exitFailure
}
logger = logger.WithLevel(conf.Log.GetLevel())
errorChan := make(chan error)
// in the future we might implement an evaluation statistics reporter
// var evalReporter statistics.Reporter
statusReporter := status.NewReporter(&conf.Cache)
var metricsReporter metrics.Reporter
if conf.Diag.Metrics.Enabled {
metricsReporter = metrics.NewReporter()
}
var diagServer *diag.Server
if conf.Diag.Enabled && (conf.Diag.Metrics.Enabled || conf.Diag.Status.Enabled) {
diagServer = diag.NewServer(&conf.Diag, statusReporter, metricsReporter, logger, errorChan)
diagServer.Listen()
}
var externalCache cache.External
if conf.Cache.IsSet() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) // give 5 sec to spin up the cache connection
defer cancel()
externalCache, err = cache.SetupExternalCache(ctx, &conf.Cache, logger)
if err != nil {
return exitFailure
}
}
sdkRegistrar := sdk.NewRegistrar(&conf, metricsReporter, statusReporter, externalCache, logger)
var httpServer *web.Server
var router *web.HttpRouter
if conf.Http.Enabled {
router = web.NewRouter(sdkRegistrar, metricsReporter, statusReporter, &conf.Http, logger)
httpServer, err = web.NewServer(router.Handler(), logger, &conf, errorChan)
if err != nil {
return exitFailure
}
httpServer.Listen()
}
var grpcServer *grpc.Server
if conf.Grpc.Enabled {
grpcServer, err = grpc.NewServer(sdkRegistrar, metricsReporter, statusReporter, &conf, logger, errorChan)
if err != nil {
return exitFailure
}
grpcServer.Listen()
}
for {
select {
case <-closeSignal:
sdkRegistrar.Close()
if router != nil {
router.Close()
}
shutDownCount := 0
if externalCache != nil {
shutDownCount++
}
if httpServer != nil {
shutDownCount++
}
if grpcServer != nil {
shutDownCount++
}
if diagServer != nil {
shutDownCount++
}
wg := sync.WaitGroup{}
wg.Add(shutDownCount)
if externalCache != nil {
go func() {
externalCache.Shutdown()
wg.Done()
}()
}
if httpServer != nil {
go func() {
httpServer.Shutdown()
wg.Done()
}()
}
if diagServer != nil {
go func() {
diagServer.Shutdown()
wg.Done()
}()
}
if grpcServer != nil {
go func() {
grpcServer.Shutdown()
wg.Done()
}()
}
wg.Wait()
return exitOk
case err = <-errorChan:
logger.Errorf("%s", err)
return exitFailure
}
}
}