-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathserver.go
More file actions
72 lines (60 loc) · 2.04 KB
/
Copy pathserver.go
File metadata and controls
72 lines (60 loc) · 2.04 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
// Copyright (c) 2026 Lerian Studio. All rights reserved.
// Use of this source code is governed by the Elastic License 2.0
// that can be found in the LICENSE file.
package bootstrap
import (
"context"
"time"
libCommons "github.com/LerianStudio/lib-commons/v5/commons"
libCommonsLog "github.com/LerianStudio/lib-observability/log"
libCommonsOtel "github.com/LerianStudio/lib-observability/tracing"
libCommonsServer "github.com/LerianStudio/lib-commons/v5/commons/server"
"github.com/gofiber/fiber/v2"
)
// Server represents the http server for CRM services.
type Server struct {
app *fiber.App
serverAddress string
logger libCommonsLog.Logger
telemetry libCommonsOtel.Telemetry
readyzHandler *ReadyzHandler
}
// ServerAddress returns is a convenience method to return the server address.
func (s *Server) ServerAddress() string {
return s.serverAddress
}
// NewServer creates an instance of Server.
func NewServer(cfg *Config, app *fiber.App, logger libCommonsLog.Logger, telemetry *libCommonsOtel.Telemetry, readyzHandler *ReadyzHandler) *Server {
return &Server{
app: app,
serverAddress: cfg.ServerAddress,
logger: logger,
telemetry: *telemetry,
readyzHandler: readyzHandler,
}
}
// Run runs the server.
func (s *Server) Run(l *libCommons.Launcher) error {
// Register lifecycle hooks for readyz
s.app.Hooks().OnListen(func(_ fiber.ListenData) error {
if s.readyzHandler != nil {
s.readyzHandler.SetServerReady()
}
return nil
})
s.app.Hooks().OnShutdown(func() error {
if s.readyzHandler != nil {
s.readyzHandler.StartDrain()
// Wait for drain delay to allow load balancers to stop sending traffic
s.logger.Log(context.Background(), libCommonsLog.LevelInfo,
"Waiting for graceful drain before shutdown",
libCommonsLog.String("drain_delay", DefaultDrainDelay.String()))
time.Sleep(DefaultDrainDelay)
}
return nil
})
libCommonsServer.NewServerManager(nil, &s.telemetry, s.logger).
WithHTTPServer(s.app, s.serverAddress).
StartWithGracefulShutdown()
return nil
}