-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathserver.go
181 lines (163 loc) · 5.61 KB
/
server.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package api
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"github.com/aws/aws-lambda-go/lambda"
"github.com/labstack/echo-contrib/echoprometheus"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
custommiddleware "github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/api/middleware"
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/api/opentelemetry"
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/config"
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/controller"
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/metric"
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/ofrep"
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/service"
"go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho"
"go.uber.org/zap"
)
// New is used to create a new instance of the API server
func New(config *config.Config,
services service.Services,
zapLog *zap.Logger,
) Server {
s := Server{
config: config,
services: services,
zapLog: zapLog,
otelService: opentelemetry.NewOtelService(),
}
s.apiEcho = echo.New()
s.initRoutes()
return s
}
// Server is the struct that represents the API server
type Server struct {
config *config.Config
apiEcho *echo.Echo
monitoringEcho *echo.Echo
services service.Services
zapLog *zap.Logger
otelService opentelemetry.OtelService
}
// initRoutes initialize the API endpoints that contain business logic and specificity for the relay proxy
func (s *Server) initRoutes() {
s.apiEcho.HideBanner = true
s.apiEcho.HidePort = true
s.apiEcho.Debug = s.config.IsDebugEnabled()
s.apiEcho.Use(otelecho.Middleware("go-feature-flag"))
s.apiEcho.Use(custommiddleware.ZapLogger(s.zapLog, s.config))
s.apiEcho.Use(middleware.BodyDumpWithConfig(middleware.BodyDumpConfig{
Skipper: func(c echo.Context) bool {
isSwagger := strings.HasPrefix(c.Request().URL.String(), "/swagger")
return isSwagger || !s.zapLog.Core().Enabled(zap.DebugLevel)
},
Handler: func(_ echo.Context, reqBody []byte, _ []byte) {
s.zapLog.Debug("Request info", zap.ByteString("request_body", reqBody))
},
}))
if s.services.Metrics != (metric.Metrics{}) {
s.apiEcho.Use(echoprometheus.NewMiddlewareWithConfig(echoprometheus.MiddlewareConfig{
Subsystem: metric.GOFFSubSystem,
Registerer: s.services.Metrics.Registry,
}))
}
s.apiEcho.Use(middleware.CORSWithConfig(middleware.DefaultCORSConfig))
s.apiEcho.Use(custommiddleware.VersionHeader(custommiddleware.VersionHeaderConfig{
Skipper: func(_ echo.Context) bool {
return s.config.DisableVersionHeader
},
RelayProxyConfig: s.config,
}))
s.apiEcho.Use(middleware.Recover())
// Init controllers
cAllFlags := controller.NewAllFlags(s.services.GOFeatureFlagService, s.services.Metrics)
cFlagEval := controller.NewFlagEval(s.services.GOFeatureFlagService, s.services.Metrics)
cFlagEvalOFREP := ofrep.NewOFREPEvaluate(s.services.GOFeatureFlagService, s.services.Metrics)
cEvalDataCollector := controller.NewCollectEvalData(
s.services.GOFeatureFlagService,
s.services.Metrics,
s.zapLog,
)
cRetrieverRefresh := controller.NewForceFlagsRefresh(
s.services.GOFeatureFlagService,
s.services.Metrics,
)
cFlagChangeAPI := controller.NewAPIFlagChange(
s.services.GOFeatureFlagService,
s.services.Metrics,
)
// Init routes
s.addGOFFRoutes(cAllFlags, cFlagEval, cEvalDataCollector, cFlagChangeAPI)
s.addOFREPRoutes(cFlagEvalOFREP)
s.addWebsocketRoutes()
s.addMonitoringRoutes()
s.addAdminRoutes(cRetrieverRefresh)
}
// Start launch the API server
func (s *Server) Start() {
// starting the monitoring server on a different port if configured
if s.monitoringEcho != nil {
go func() {
addressMonitoring := fmt.Sprintf("0.0.0.0:%d", s.config.MonitoringPort)
s.zapLog.Info(
"Starting monitoring",
zap.String("address", addressMonitoring))
err := s.monitoringEcho.Start(addressMonitoring)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
s.zapLog.Fatal("Error starting monitoring", zap.Error(err))
}
}()
defer func() { _ = s.monitoringEcho.Close() }()
}
// start the OpenTelemetry tracing service
err := s.otelService.Init(context.Background(), s.zapLog, *s.config)
if err != nil {
s.zapLog.Error(
"error while initializing OTel, continuing without tracing enabled",
zap.Error(err),
)
// we can continue because otel is not mandatory to start the server
}
// starting the main application
if s.config.ListenPort == 0 {
s.config.ListenPort = 1031
}
address := fmt.Sprintf("0.0.0.0:%d", s.config.ListenPort)
s.zapLog.Info(
"Starting go-feature-flag relay proxy ...",
zap.String("address", address),
zap.String("version", s.config.Version))
err = s.apiEcho.Start(address)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
s.zapLog.Fatal("Error starting relay proxy", zap.Error(err))
}
}
// StartAwsLambda is starting the relay proxy as an AWS Lambda
func (s *Server) StartAwsLambda() {
lambda.Start(s.getLambdaHandler())
}
func (s *Server) getLambdaHandler() interface{} {
handlerMngr := newAwsLambdaHandlerManager(s.apiEcho)
return handlerMngr.GetAdapter(s.config.AwsLambdaAdapter)
}
// Stop shutdown the API server
func (s *Server) Stop(ctx context.Context) {
err := s.otelService.Stop(ctx)
if err != nil {
s.zapLog.Error("impossible to stop otel", zap.Error(err))
}
if s.monitoringEcho != nil {
err = s.monitoringEcho.Close()
if err != nil {
s.zapLog.Fatal("impossible to stop monitoring", zap.Error(err))
}
}
err = s.apiEcho.Close()
if err != nil {
s.zapLog.Fatal("impossible to stop go-feature-flag relay proxy", zap.Error(err))
}
}