-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathmain.go
112 lines (96 loc) Β· 3.88 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
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/spf13/pflag"
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/api"
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/config"
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/docs"
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/log"
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/metric"
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/service"
"github.com/thomaspoignant/go-feature-flag/notifier"
"go.uber.org/zap"
)
// version, releaseDate are override by the makefile during the build.
var version = "localdev"
const banner = `ββββββββ βββββββββββββββββββββββββββββ ββββββββββββββββ
ββββββββ βββββββββββββββββββββββββββββ ββββββββββββββββ
βββββββββββββββββββββββββ ββββββββββββββββββββ
βββββββββββββββββββββββββ ββββββββββββββββββββ
GO Feature Flag Relay Proxy - Version %s
_____________________________________________`
// @title GO Feature Flag relay proxy endpoints
// @description.markdown
// @contact.name GO feature flag relay proxy
// @contact.url https://gofeatureflag.org
// @contact.email [email protected]
// @license.name MIT
// @license.url https://github.com/thomaspoignant/go-feature-flag/blob/main/LICENSE
// @x-logo {"url":"https://raw.githubusercontent.com/thomaspoignant/go-feature-flag/main/logo_128.png"}
// @BasePath /
// @securitydefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization
// @description Use configured APIKeys in yaml config as authorization keys, disabled when this yaml config is not set.
func main() {
// Init pFlag for config file
f := pflag.NewFlagSet("config", pflag.ContinueOnError)
f.String("config", "", "Location of your config file")
_ = f.Parse(os.Args[1:])
// Init logger
logger := log.InitLogger()
defer func() { _ = logger.ZapLogger.Sync() }()
// Loading the configuration in viper
proxyConf, err := config.New(f, logger.ZapLogger, version)
if err != nil {
logger.ZapLogger.Fatal("error while reading configuration", zap.Error(err))
}
if err := proxyConf.IsValid(); err != nil {
logger.ZapLogger.Fatal("configuration error", zap.Error(err))
}
if !proxyConf.HideBanner {
fmt.Printf(banner+"\n", version)
}
// Update the logger's format and level from the config
logger.Update(proxyConf.LogFormat, proxyConf.ZapLogLevel())
// Init swagger
docs.SwaggerInfo.Version = proxyConf.Version
docs.SwaggerInfo.Host = fmt.Sprintf("%s:%d", proxyConf.Host, proxyConf.ListenPort)
// Init services
metricsV2, err := metric.NewMetrics()
if err != nil {
logger.ZapLogger.Error("impossible to initialize prometheus metrics", zap.Error(err))
}
wsService := service.NewWebsocketService()
defer wsService.Close() // close all the open connections
prometheusNotifier := metric.NewPrometheusNotifier(metricsV2)
proxyNotifier := service.NewNotifierWebsocket(wsService)
goff, err := service.NewGoFeatureFlagClient(proxyConf, logger.ZapLogger, []notifier.Notifier{
prometheusNotifier,
proxyNotifier,
})
if err != nil {
panic(err)
}
services := service.Services{
MonitoringService: service.NewMonitoring(goff),
WebsocketService: wsService,
GOFeatureFlagService: goff,
Metrics: metricsV2,
}
// Init API server
apiServer := api.New(proxyConf, services, logger.ZapLogger)
if proxyConf.StartAsAwsLambda {
apiServer.StartAwsLambda()
} else {
defer func() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
apiServer.Stop(ctx)
}()
apiServer.Start()
}
}