-
-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathmain.go
More file actions
130 lines (113 loc) Β· 4.69 KB
/
main.go
File metadata and controls
130 lines (113 loc) Β· 4.69 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 (
"context"
"fmt"
"os"
"time"
promversion "github.com/prometheus/common/version"
"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 is overridden by GoReleaser 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 contact@gofeatureflag.org
// @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.
// @securitydefinitions.apikey XApiKeyAuth
// @in header
// @name X-API-Key
// @description Use configured APIKeys in yaml config as authorization keys via X-API-Key header,
// @description 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))
}
defer func() {
if err := proxyConf.StopConfigChangeWatcher(); err != nil {
logger.ZapLogger.Error("error while stopping the configuration watcher", 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.SwaggerHost(), proxyConf.ServerPort(logger.ZapLogger))
// Set the version for the prometheus version collector
promversion.Version = version
// Initialize metrics
metricsV2, err := metric.NewMetrics(metric.MetricsOpts{
EnableBulkMetricFlagNames: proxyConf.EnableBulkMetricFlagNames,
})
if err != nil {
logger.ZapLogger.Error("impossible to initialize prometheus metrics", zap.Error(err))
}
// Init services
wsService := service.NewWebsocketService()
defer wsService.Close() // close all the open connections
prometheusNotifier := metric.NewPrometheusNotifier(metricsV2)
proxyNotifier := service.NewNotifierWebsocket(wsService)
flagsetManager, err := service.NewFlagsetManager(proxyConf, logger.ZapLogger, []notifier.Notifier{
prometheusNotifier,
proxyNotifier,
})
if err != nil {
logger.ZapLogger.Error(
"impossible to start GO Feature Flag, we are not able to initialize the retrieval of flags",
zap.Error(err),
)
return
}
services := service.Services{
MonitoringService: service.NewMonitoring(flagsetManager),
WebsocketService: wsService,
FlagsetManager: flagsetManager,
Metrics: metricsV2,
}
// Init API server
apiServer := api.New(proxyConf, services, logger.ZapLogger)
defer func() {
logger.ZapLogger.Info("Stopping API server")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
apiServer.Stop(ctx)
}()
apiServer.StartWithContext(context.Background())
}