-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
439 lines (363 loc) · 14.6 KB
/
service.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package servicefoundation
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/Travix-International/go-servicefoundation/env"
)
const (
envCORSOrigins = "CORS_ORIGINS"
envHTTPpPort = "HTTPPORT"
envLogMinFilter = "LOG_MINFILTER"
envAppName = "APP_NAME"
envServerName = "SERVER_NAME"
envDeployEnvironment = "DEPLOY_ENVIRONMENT"
defaultHTTPPort = 8080
defaultLogMinFilter = "Warning"
publicSubsystem = "public"
)
type (
// ShutdownFunc is a function signature for the shutdown function.
ShutdownFunc func(log Logger)
// ServiceGlobals contains basic service properties, like name, deployment environment and version number.
ServiceGlobals struct {
AppName string
GroupName string
ServerName string
DeployEnvironment string
VersionNumber string
}
// ServiceOptions contains value and references used by the Service implementation. The contents of ServiceOptions
// can be used to customize or extend ServiceFoundation.
ServiceOptions struct {
Globals ServiceGlobals
Port int
ReadinessPort int
InternalPort int
LogFactory LogFactory
Metrics Metrics
RouterFactory RouterFactory
MiddlewareWrapper MiddlewareWrapper
Handlers *Handlers
WrapHandler WrapHandler
VersionBuilder VersionBuilder
ServiceStateReader ServiceStateReader
ShutdownFunc ShutdownFunc
ExitFunc ExitFunc
ServerTimeout time.Duration
IdleTimeout time.Duration
UsePublicRootHandler bool
}
// ServiceStateReader contains state methods used by the service's handler implementations.
ServiceStateReader interface {
IsLive() bool
IsReady() bool
IsHealthy() bool
}
// Service is the main interface for ServiceFoundation and is used to define routing and running the service.
Service interface {
Run(ctx context.Context)
AddRoute(name string, routes []string, methods []string, middlewares []Middleware, metaFunc MetaFunc, handler Handle)
}
serviceStateReaderImpl struct {
}
serviceImpl struct {
globals ServiceGlobals
serverTimeout time.Duration
idleTimeout time.Duration
port int
readinessPort int
internalPort int
logFactory LogFactory
log Logger
metrics Metrics
publicRouter *Router
readinessRouter *Router
internalRouter *Router
handlers *Handlers
wrapHandler WrapHandler
versionBuilder VersionBuilder
stateReader ServiceStateReader
shutdownFunc ShutdownFunc
exitFunc ExitFunc
quitting bool
sendChan chan bool
receiveChan chan bool
usePublicRootHandler bool
}
)
// DefaultMiddlewares contains the default middleware wrappers for the predefined service endpoints.
var DefaultMiddlewares = []Middleware{PanicTo500, NoCaching}
// NewService creates and returns a Service that uses environment variables for default configuration.
func NewService(group, name string, allowedMethods []string, shutdownFunc ShutdownFunc, version BuildVersion,
meta map[string]string) Service {
opt := NewServiceOptions(group, name, allowedMethods, shutdownFunc, version, meta)
return NewCustomService(opt)
}
// NewServiceOptions creates and returns ServiceOptions that use environment variables for default configuration.
func NewServiceOptions(group, name string, allowedMethods []string, shutdownFunc ShutdownFunc, version BuildVersion,
meta map[string]string) ServiceOptions {
appName := env.OrDefault(envAppName, name)
serverName := env.OrDefault(envServerName, name)
deployEnvironment := env.OrDefault(envDeployEnvironment, "UNKNOWN")
corsOptions := CORSOptions{
AllowedOrigins: env.ListOrDefault(envCORSOrigins, []string{"*"}),
AllowedMethods: allowedMethods,
}
globals := ServiceGlobals{
AppName: appName,
GroupName: group,
ServerName: serverName,
DeployEnvironment: deployEnvironment,
VersionNumber: version.VersionNumber,
}
serviceMeta := createServiceMeta(meta, globals)
logFactory := NewLogFactory(env.OrDefault(envLogMinFilter, defaultLogMinFilter), serviceMeta)
logger := logFactory.NewLogger(meta)
metrics := NewMetrics(name, logger)
versionBuilder := NewVersionBuilder(version)
middlewareWrapper := NewMiddlewareWrapper(logFactory, metrics, &corsOptions, globals)
stateReader := NewServiceStateReader()
exitFunc := NewExitFunc(logger, shutdownFunc)
port := env.AsInt(envHTTPpPort, defaultHTTPPort)
opt := ServiceOptions{
Globals: globals,
ServerTimeout: time.Second * 30,
IdleTimeout: time.Second * 30,
Port: port,
ReadinessPort: port + 1,
InternalPort: port + 2,
MiddlewareWrapper: middlewareWrapper,
RouterFactory: NewRouterFactory(),
LogFactory: logFactory,
Metrics: metrics,
VersionBuilder: versionBuilder,
ServiceStateReader: stateReader,
ExitFunc: exitFunc,
UsePublicRootHandler: true,
}
opt.SetHandlers()
return opt
}
func createServiceMeta(baseMeta map[string]string, globals ServiceGlobals) map[string]string {
serviceMeta := make(map[string]string)
serviceMeta["entry.applicationgroup"] = globals.GroupName
serviceMeta["entry.applicationname"] = globals.AppName
serviceMeta["entry.applicationversion"] = globals.VersionNumber
serviceMeta["entry.machinename"] = globals.ServerName
return combineMetas(baseMeta, serviceMeta)
}
// NewCustomService allows you to customize ServiceFoundation using your own implementations of factories.
func NewCustomService(options ServiceOptions) Service {
return &serviceImpl{
globals: options.Globals,
serverTimeout: options.ServerTimeout,
idleTimeout: options.IdleTimeout,
port: options.Port,
readinessPort: options.ReadinessPort,
internalPort: options.InternalPort,
logFactory: options.LogFactory,
log: options.LogFactory.NewLogger(make(map[string]string)),
metrics: options.Metrics,
publicRouter: options.RouterFactory.NewRouter(),
readinessRouter: options.RouterFactory.NewRouter(),
internalRouter: options.RouterFactory.NewRouter(),
handlers: options.Handlers,
wrapHandler: options.WrapHandler,
versionBuilder: options.VersionBuilder,
stateReader: options.ServiceStateReader,
exitFunc: options.ExitFunc,
sendChan: make(chan bool, 1),
receiveChan: make(chan bool, 1),
usePublicRootHandler: options.UsePublicRootHandler,
}
}
// NewExitFunc returns a new exit function. It wraps the shutdownFunc and executed an os.exit after the shutdown is
// completed with a slight delay, giving the quit handler a chance to return a status.
func NewExitFunc(log Logger, shutdownFunc ShutdownFunc) func(int) {
return func(code int) {
log.Debug("ServiceExit", "Performing service exit")
go func() {
if shutdownFunc != nil {
log.Debug("ShutdownFunc", "Calling shutdown func")
shutdownFunc(log)
}
if code != 0 {
time.Sleep(500 * time.Millisecond)
}
log.Debug("ServiceExit", "Calling os.Exit(%v)", code)
os.Exit(code)
}()
// Allow the go-routine to be spawned
time.Sleep(1 * time.Millisecond)
}
}
// NewServiceStateReader instantiates a new basic ServiceStateReader implementation, which always returns true
// for it's state methods.
func NewServiceStateReader() ServiceStateReader {
return &serviceStateReaderImpl{}
}
/* ServiceStateReader implementation */
func (s *serviceStateReaderImpl) IsLive() bool {
return true
}
func (s *serviceStateReaderImpl) IsReady() bool {
return true
}
func (s *serviceStateReaderImpl) IsHealthy() bool {
return true
}
/* ServiceOptions implementation */
// SetHandlers is used to update the handler references in ServiceOptions to use the correct middleware and state.
func (o *ServiceOptions) SetHandlers() {
factory := NewServiceHandlerFactory(o.MiddlewareWrapper, o.VersionBuilder, o.ServiceStateReader, o.ExitFunc)
o.Handlers = factory.NewHandlers()
o.WrapHandler = factory
}
/* Service implementation */
func (s *serviceImpl) Run(ctx context.Context) {
s.log.Info("Service", "%s: %s", s.globals.AppName, s.versionBuilder.ToString())
sigs := make(chan os.Signal, 1)
done := make(chan bool, 1)
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
go func() {
select {
case <-s.receiveChan:
s.log.Debug("UnexpectedShutdownReceived", "Server shut down unexpectedly")
// One of the servers has shut down unexpectedly. Because this makes the whole service unreliable, shutdown.
break
case <-ctx.Done():
s.log.Debug("ServiceCancel", "Cancellation request received")
// Shutdown any running http servers
s.quitting = true
s.sendChan <- true
break
case <-sigs:
s.log.Debug("GracefulShutdown", "Handling Sigterm/SigInt")
break
}
if !s.quitting {
// Some other go-routine is already taking care of the shutdown
s.quitting = true
s.sendChan <- true
}
// Trigger graceful shutdown
s.exitFunc(0)
done <- true
}()
s.runReadinessServer()
s.runInternalServer()
s.runPublicServer()
<-done // Wait for our shutdown
// since service.ExitFunc calls os.Exit(), we'll never get here
}
func (s *serviceImpl) AddRoute(name string, routes []string, methods []string, middlewares []Middleware, metaFunc MetaFunc, handler Handle) {
s.addRouteWithMetaAndPreFlight(s.publicRouter, publicSubsystem, name, routes, methods, middlewares, metaFunc, handler)
}
func (s *serviceImpl) addRoute(router *Router, subsystem, name string, routes []string, methods []string, middlewares []Middleware, handler Handle) {
defaultMetaFunc := func(_ *http.Request, _ RouterParams) map[string]string {
return make(map[string]string)
}
for _, path := range routes {
wrappedHandler := s.wrapHandler.Wrap(subsystem, name, middlewares, handler, defaultMetaFunc)
for _, method := range methods {
router.Router.Handle(method, path, wrappedHandler)
}
}
}
func (s *serviceImpl) addRouteWithMetaAndPreFlight(router *Router, subsystem, name string, routes []string, methods []string,
middlewares []Middleware, metaFunc MetaFunc, handler Handle) {
for _, path := range routes {
wrappedHandler := s.wrapHandler.Wrap(subsystem, name, middlewares, handler, metaFunc)
preFlightHandled := false
for _, method := range methods {
router.Router.Handle(method, path, wrappedHandler)
preFlightHandled = preFlightHandled || method == http.MethodOptions
}
if preFlightHandled {
continue
}
s.addPreFlightHandle(subsystem, name, middlewares, metaFunc, router, path)
}
}
func (s *serviceImpl) addPreFlightHandle(subsystem string, name string, middlewares []Middleware, metaFunc MetaFunc,
router *Router, path string) {
preFlightMiddlewares := []Middleware{Counter}
for _, m := range middlewares {
if m != CORS {
continue
}
preFlightMiddlewares = append([]Middleware{CORS}, preFlightMiddlewares...)
}
preFlightHandler := s.handlers.PreFlightHandler.NewPreFlightHandler()
wrappedPreFlightHandler := s.wrapHandler.Wrap(subsystem, fmt.Sprintf("%v-preflight", name),
preFlightMiddlewares, preFlightHandler, metaFunc)
router.Router.Handle(http.MethodOptions, path, wrappedPreFlightHandler)
}
func (s *serviceImpl) runHTTPServer(port int, router *Router) {
addr := fmt.Sprintf(":%v", port)
svr := &http.Server{
ReadTimeout: s.serverTimeout,
WriteTimeout: s.serverTimeout,
IdleTimeout: s.idleTimeout,
Addr: addr,
Handler: router.Router,
}
go func() {
// Blocking until the server stops.
svr.ListenAndServe()
// Notify the service that the server has stopped.
s.receiveChan <- true
}()
go func() {
// Monitor sender channel and close server on signal.
select {
case sig := <-s.sendChan:
// Properly close the server if possible.
if svr != nil {
svr.Close()
svr = nil
}
// Continue sending the message
s.sendChan <- sig
break
}
}()
}
// RunReadinessServer runs the readiness service as a go-routine
func (s *serviceImpl) runReadinessServer() {
const subsystem = "readiness"
router := s.readinessRouter
s.addRoute(router, subsystem, "root", []string{"/"}, MethodsForGet, DefaultMiddlewares, s.handlers.RootHandler.NewRootHandler())
s.addRoute(router, subsystem, "liveness", []string{"/service/liveness"}, MethodsForGet, DefaultMiddlewares, s.handlers.LivenessHandler.NewLivenessHandler())
s.addRoute(router, subsystem, "readiness", []string{"/service/readiness"}, MethodsForGet, DefaultMiddlewares, s.handlers.ReadinessHandler.NewReadinessHandler())
s.log.Info("RunReadinessServer", "%s %s running on localhost:%d.", s.globals.AppName, subsystem, s.readinessPort)
s.runHTTPServer(s.readinessPort, router)
}
// RunInternalServer runs the internal service as a go-routine
func (s *serviceImpl) runInternalServer() {
const subsystem = "internal"
router := s.internalRouter
s.addRoute(router, subsystem, "root", []string{"/"}, MethodsForGet, DefaultMiddlewares, s.handlers.RootHandler.NewRootHandler())
s.addRoute(router, subsystem, "health_check", []string{"/health_check", "/healthz"}, MethodsForGet, DefaultMiddlewares, s.handlers.HealthHandler.NewHealthHandler())
s.addRoute(router, subsystem, "metrics", []string{"/metrics"}, MethodsForGet, DefaultMiddlewares, s.handlers.MetricsHandler.NewMetricsHandler())
s.addRoute(router, subsystem, "quit", []string{"/quit"}, MethodsForGet, DefaultMiddlewares, s.handlers.QuitHandler.NewQuitHandler())
s.log.Info("RunInternalServer", "%s %s running on localhost:%d.", s.globals.AppName, subsystem, s.internalPort)
s.runHTTPServer(s.internalPort, router)
}
// RunPublicServer runs the public service on the current thread.
func (s *serviceImpl) runPublicServer() {
router := s.publicRouter
if s.usePublicRootHandler {
s.addRoute(router, publicSubsystem, "root", []string{"/"}, MethodsForGet, DefaultMiddlewares, s.handlers.RootHandler.NewRootHandler())
}
s.addRoute(router, publicSubsystem, "version", []string{"/service/version"}, MethodsForGet, DefaultMiddlewares, s.handlers.VersionHandler.NewVersionHandler())
s.addRoute(router, publicSubsystem, "liveness", []string{"/service/liveness"}, MethodsForGet, DefaultMiddlewares, s.handlers.LivenessHandler.NewLivenessHandler())
s.addRoute(router, publicSubsystem, "readiness", []string{"/service/readiness"}, MethodsForGet, DefaultMiddlewares, s.handlers.ReadinessHandler.NewReadinessHandler())
s.log.Info("RunPublicService", "%s %s running on localhost:%d.", s.globals.AppName, publicSubsystem, s.port)
s.runHTTPServer(s.port, router)
}