-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
211 lines (173 loc) · 4.69 KB
/
app.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
package freja
import (
"context"
"encoding/json"
"fmt"
"github.com/kayx-org/freja/component"
"github.com/kayx-org/freja/env"
"github.com/kayx-org/freja/healthcheck"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
//go:generate moq -out health_calculator_mock_test.go . healthCalculator
type healthCalculator interface {
Add(healthcheck.HealthChecker)
Calculate() (bool, []Status)
}
//go:generate moq -out server_mock_test.go . Server
type Server interface {
ListenAndServe() error
Shutdown(context.Context) error
}
type OptionApp func(*App)
type App struct {
healthCalculator healthCalculator
server Server
logger Logger
gracefulShutdownTimeout time.Duration
meddlers []Middleware
cancel context.CancelFunc
osSignal chan os.Signal // listen when the service is asked to shutdown
gracefulStop chan bool // Use to initiate the graceful shutdown
}
func NewApp(healthCalculator healthCalculator, logger Logger, options ...OptionApp) *App {
return &App{
healthCalculator: healthCalculator,
logger: logger,
meddlers: make([]Middleware, 0),
osSignal: make(chan os.Signal, 1),
gracefulStop: make(chan bool, 1),
gracefulShutdownTimeout: time.Second * 10,
}
}
func New(options ...OptionApp) *App {
app := NewApp(NewHealthCalculator(), component.NewLogger())
for _, o := range options {
o(app)
}
return app
}
func OptionGracefulShutdownTimeout(gracefulShutdownTimeout time.Duration) OptionApp {
return func(a *App) {
a.gracefulShutdownTimeout = gracefulShutdownTimeout
}
}
func OptionCustomServer(server Server) OptionApp {
return func(a *App) {
a.server = server
}
}
func (a *App) GetEnvOrDie(key string) string {
res, err := env.GetEnvOrErr(key)
if err != nil {
a.logger.Fatal(err)
}
return res
}
func (a *App) GetEnvAsIntOrDie(key string) int {
res, err := env.GetEnvAsIntOrErr(key)
if err != nil {
a.logger.Fatal(err)
}
return res
}
// AddMiddleware adds another middleware, and if it does implement the interface HealthChecker
// it'll add as a HealCheck as well
func (a *App) AddMiddleware(m Middleware) {
a.meddlers = append(a.meddlers, m)
if h, ok := m.(healthcheck.HealthChecker); ok {
a.AddHealthCheck(h)
}
}
func (a *App) AddHealthCheck(h healthcheck.HealthChecker) {
if a.healthCalculator != nil {
a.healthCalculator.Add(h)
}
}
// HealthCheck returns an error to indicate that the service is not healthy
func (a *App) HealthCheck() ([]byte, error) {
if a.healthCalculator != nil {
status, summary := a.healthCalculator.Calculate()
if marshalled, err := json.Marshal(summary); err != nil {
a.logger.Errorf("error while marshaling the health-check : %s", err)
return []byte(""), fmt.Errorf("unable to encode the summary: %w", err)
} else if !status {
return marshalled, fmt.Errorf("unhealthy")
} else {
return marshalled, nil
}
}
return []byte(""), nil
}
func (a *App) WithServer(s Server) {
a.server = s
}
func (a *App) Server(h http.Handler) {
a.server = component.NewServer(h,
component.OptionErrorLogWriter(
NewLogWrite(a.logger, "error")))
}
func (a *App) Logger() Logger {
return a.logger
}
func (a *App) init() error {
for _, mid := range a.meddlers {
if err := mid.Init(); err != nil {
return fmt.Errorf("unable to run Init(): %w", err)
}
}
signal.Notify(a.osSignal, syscall.SIGTERM)
signal.Notify(a.osSignal, syscall.SIGINT)
go func() {
sig := <-a.osSignal
a.logger.Infof("signal caught: %s", sig)
a.gracefulStop <- true
}()
return nil
}
func (a *App) Start(ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx)
a.cancel = cancel
if err := a.init(); err != nil {
return err
}
for _, mid := range a.meddlers {
go func(mid Middleware) {
if err := mid.Run(ctx); err != nil {
a.logger.Errorf("unable to run middleware: %s", err)
}
}(mid)
}
time.Sleep(time.Millisecond * 200)
if a.server != nil {
go func() {
a.logger.Info("starting server")
if err := a.server.ListenAndServe(); err != nil {
a.logger.Fatalf("unable to run the server: %s", err)
}
}()
}
a.stop(ctx)
a.logger.Info("shutdown finalized")
return nil
}
func (a *App) stop(ctx context.Context) {
<-a.gracefulStop
a.logger.Info("graceful shutdown initiated")
a.cancel()
ctx, cancel := context.WithTimeout(ctx, a.gracefulShutdownTimeout)
defer cancel()
if a.server != nil {
if err := a.server.Shutdown(ctx); err != nil {
a.logger.Errorf("error gracefully stopping server: %s", err)
}
}
for _, mid := range a.meddlers {
if err := mid.Stop(ctx); err != nil {
a.logger.Errorf("error gracefully stopping middleware: %s", err)
}
}
}