-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
130 lines (112 loc) · 3.38 KB
/
Copy pathmain.go
File metadata and controls
130 lines (112 loc) · 3.38 KB
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
package main
import (
"flag"
"github.com/configcat/configcat-proxy/cache"
"github.com/configcat/configcat-proxy/config"
"github.com/configcat/configcat-proxy/diag"
"github.com/configcat/configcat-proxy/diag/status"
"github.com/configcat/configcat-proxy/diag/telemetry"
"github.com/configcat/configcat-proxy/grpc"
"github.com/configcat/configcat-proxy/log"
"github.com/configcat/configcat-proxy/sdk"
"github.com/configcat/configcat-proxy/web"
"os"
"os/signal"
"sync"
"syscall"
)
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("ConfigCat Proxy v%s starting...", sdk.Version())
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)
shutdownFuncs := make([]func(), 0)
// in the future we might implement an evaluation statistics reporter
// var evalReporter statistics.Reporter
statusReporter := status.NewReporter(&conf.Cache)
telemetryReporter := telemetry.NewReporter(&conf.Diag, sdk.Version(), logger)
shutdownFuncs = append(shutdownFuncs, func() { telemetryReporter.Shutdown() })
var diagServer *diag.Server
if conf.Diag.ShouldRunDiagServer() {
diagServer = diag.NewServer(&conf.Diag, telemetryReporter, statusReporter, logger, errorChan)
diagServer.Listen()
shutdownFuncs = append(shutdownFuncs, func() { diagServer.Shutdown() })
}
var externalCache cache.External
if conf.Cache.IsSet() {
externalCache, err = cache.SetupExternalCache(&conf.Cache, telemetryReporter, logger)
if err != nil {
return exitFailure
}
shutdownFuncs = append(shutdownFuncs, func() { externalCache.Shutdown() })
}
sdkRegistrar, err := sdk.NewRegistrar(&conf, telemetryReporter, statusReporter, externalCache, logger)
if err != nil {
return exitFailure
}
var httpServer *web.Server
var router *web.HttpRouter
if conf.Http.Enabled {
router = web.NewRouter(sdkRegistrar, telemetryReporter, statusReporter, &conf.Http, &conf.Profile, logger)
httpServer, err = web.NewServer(router, logger, &conf, errorChan)
if err != nil {
return exitFailure
}
httpServer.Listen()
shutdownFuncs = append(shutdownFuncs, func() { httpServer.Shutdown() })
}
var grpcServer *grpc.Server
if conf.Grpc.Enabled {
grpcServer, err = grpc.NewServer(sdkRegistrar, telemetryReporter, statusReporter, &conf, logger, errorChan)
if err != nil {
return exitFailure
}
grpcServer.Listen()
shutdownFuncs = append(shutdownFuncs, func() { grpcServer.Shutdown() })
}
for {
select {
case <-closeSignal:
logger.Reportf("shutdown requested...")
sdkRegistrar.Close()
if router != nil {
router.Close()
}
wg := sync.WaitGroup{}
wg.Add(len(shutdownFuncs))
for _, fn := range shutdownFuncs {
go func(f func()) {
f()
wg.Done()
}(fn)
}
wg.Wait()
return exitOk
case err = <-errorChan:
logger.Errorf("%s", err)
return exitFailure
}
}
}