-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathoptions.go
More file actions
343 lines (272 loc) · 7.96 KB
/
options.go
File metadata and controls
343 lines (272 loc) · 7.96 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
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
package httpserver
import (
"crypto/tls"
"errors"
"fmt"
"log/slog"
"net/http"
"sync"
"time"
"github.com/julienschmidt/httprouter"
)
// Option is a type alias for a function that configures the HTTP httpServer instance.
type Option func(*config) error
// WithRouter replaces the default router used by the httpServer (mostly used for test purposes with a mock router).
func WithRouter(r *httprouter.Router) Option {
return func(cfg *config) error {
if r == nil {
return errors.New("router is required")
}
cfg.router = r
return nil
}
}
// WithServerAddr sets the address the httpServer will bind to.
func WithServerAddr(addr string) Option {
return func(cfg *config) error {
err := validateAddr(addr)
if err != nil {
return err
}
cfg.serverAddr = addr
return nil
}
}
// WithRequestTimeout sets a time limit for all routes after which a request receives a 503 Service Unavailable.
// Alternatively a custom timeout handler like http.TimeoutHandler can be added via WithMiddlewareFn().
func WithRequestTimeout(timeout time.Duration) Option {
return func(cfg *config) error {
if timeout <= 0 {
return errors.New("invalid requestTimeout")
}
cfg.requestTimeout = timeout
return nil
}
}
// WithServerReadHeaderTimeout sets the read header timeout.
func WithServerReadHeaderTimeout(timeout time.Duration) Option {
return func(cfg *config) error {
if timeout <= 0 {
return errors.New("invalid serverReadHeaderTimeout")
}
cfg.serverReadHeaderTimeout = timeout
return nil
}
}
// WithServerReadTimeout sets the read timeout.
func WithServerReadTimeout(timeout time.Duration) Option {
return func(cfg *config) error {
if timeout <= 0 {
return errors.New("invalid serverReadTimeout")
}
cfg.serverReadTimeout = timeout
return nil
}
}
// WithServerWriteTimeout sets the write timeout.
func WithServerWriteTimeout(timeout time.Duration) Option {
return func(cfg *config) error {
if timeout <= 0 {
return errors.New("invalid serverWriteTimeout")
}
cfg.serverWriteTimeout = timeout
return nil
}
}
// WithShutdownTimeout sets the shutdown timeout.
func WithShutdownTimeout(timeout time.Duration) Option {
return func(cfg *config) error {
if timeout <= 0 {
return errors.New("invalid shutdownTimeout")
}
cfg.shutdownTimeout = timeout
return nil
}
}
// WithShutdownWaitGroup sets the shared waiting group to communicate externally when the server is shutdown.
func WithShutdownWaitGroup(wg *sync.WaitGroup) Option {
return func(cfg *config) error {
if wg == nil {
return errors.New("shutdownWaitGroup is required")
}
cfg.shutdownWaitGroup = wg
return nil
}
}
// WithShutdownSignalChan sets the shared channel uset to signal a shutdown.
// When the channel signal is received the server will initiate the shutdown process.
func WithShutdownSignalChan(ch chan struct{}) Option {
return func(cfg *config) error {
if ch == nil {
return errors.New("shutdownSignalChan is required")
}
cfg.shutdownSignalChan = ch
return nil
}
}
// WithTLSCertData enable TLS with the given certificate and key data.
func WithTLSCertData(pemCert, pemKey []byte) Option {
return func(cfg *config) error {
cert, err := tls.X509KeyPair(pemCert, pemKey)
if err != nil {
return fmt.Errorf("failed configuring TLS: %w", err)
}
cfg.tlsConfig = &tls.Config{
MinVersion: tls.VersionTLS12,
Certificates: []tls.Certificate{cert},
}
return nil
}
}
// WithEnableDefaultRoutes sets the default routes to be enabled on the server.
func WithEnableDefaultRoutes(ids ...DefaultRoute) Option {
return func(cfg *config) error {
cfg.defaultEnabledRoutes = ids
return nil
}
}
// WithEnableAllDefaultRoutes enables all default routes on the server.
func WithEnableAllDefaultRoutes() Option {
return func(cfg *config) error {
cfg.defaultEnabledRoutes = allDefaultRoutes()
return nil
}
}
// WithIndexHandlerFunc replaces the index handler.
func WithIndexHandlerFunc(handler IndexHandlerFunc) Option {
return func(cfg *config) error {
if handler == nil {
return errors.New("indexHandlerFunc is required")
}
cfg.indexHandlerFunc = handler
return nil
}
}
// WithIPHandlerFunc replaces the default ip handler function.
func WithIPHandlerFunc(handler http.HandlerFunc) Option {
return func(cfg *config) error {
if handler == nil {
return errors.New("ipHandlerFunc is required")
}
cfg.ipHandlerFunc = handler
return nil
}
}
// WithMetricsHandlerFunc replaces the default metrics handler function.
func WithMetricsHandlerFunc(handler http.HandlerFunc) Option {
return func(cfg *config) error {
if handler == nil {
return errors.New("metricsHandlerFunc is required")
}
cfg.metricsHandlerFunc = handler
return nil
}
}
// WithPingHandlerFunc replaces the default ping handler function.
func WithPingHandlerFunc(handler http.HandlerFunc) Option {
return func(cfg *config) error {
if handler == nil {
return errors.New("pingHandlerFunc is required")
}
cfg.pingHandlerFunc = handler
return nil
}
}
// WithPProfHandlerFunc replaces the default pprof handler function.
func WithPProfHandlerFunc(handler http.HandlerFunc) Option {
return func(cfg *config) error {
if handler == nil {
return errors.New("pprofHandlerFunc is required")
}
cfg.pprofHandlerFunc = handler
return nil
}
}
// WithStatusHandlerFunc replaces the default status handler function.
func WithStatusHandlerFunc(handler http.HandlerFunc) Option {
return func(cfg *config) error {
if handler == nil {
return errors.New("statusHandlerFunc is required")
}
cfg.statusHandlerFunc = handler
return nil
}
}
// WithTraceIDHeaderName overrides the default trace id header name.
func WithTraceIDHeaderName(name string) Option {
return func(cfg *config) error {
if name == "" {
return errors.New("traceIDHeaderName is required")
}
cfg.traceIDHeaderName = name
return nil
}
}
// WithRedactFn set the function used to redact HTTP request and response dumps in the logs.
func WithRedactFn(fn RedactFn) Option {
return func(cfg *config) error {
cfg.redactFn = fn
return nil
}
}
// WithMiddlewareFn adds one or more middleware handler functions to all routes (endpoints).
// These middleware handlers are applied in the provided order after the default ones and before the custom route ones.
func WithMiddlewareFn(fn ...MiddlewareFn) Option {
return func(cfg *config) error {
cfg.middleware = append(cfg.middleware, fn...)
return nil
}
}
// WithNotFoundHandlerFunc http handler called when no matching route is found.
func WithNotFoundHandlerFunc(handler http.HandlerFunc) Option {
return func(cfg *config) error {
if handler == nil {
return errors.New("notFoundHandlerFunc is required")
}
cfg.notFoundHandlerFunc = handler
return nil
}
}
// WithMethodNotAllowedHandlerFunc http handler called when a request cannot be routed.
func WithMethodNotAllowedHandlerFunc(handler http.HandlerFunc) Option {
return func(cfg *config) error {
if handler == nil {
return errors.New("methodNotAllowedHandlerFunc is required")
}
cfg.methodNotAllowedHandlerFunc = handler
return nil
}
}
// WithPanicHandlerFunc http handler to handle panics recovered from http handlers.
func WithPanicHandlerFunc(handler http.HandlerFunc) Option {
return func(cfg *config) error {
if handler == nil {
return errors.New("panicHandlerFunc is required")
}
cfg.panicHandlerFunc = handler
return nil
}
}
// WithoutRouteLogger disables the logger handler for all routes.
func WithoutRouteLogger() Option {
return func(cfg *config) error {
cfg.disableRouteLogger = true
return nil
}
}
// WithoutDefaultRouteLogger disables the logger handler for the specified default routes.
func WithoutDefaultRouteLogger(routes ...DefaultRoute) Option {
return func(cfg *config) error {
for _, route := range routes {
cfg.disableDefaultRouteLogger[route] = true
}
return nil
}
}
// WithLogger overrides the default logger.
func WithLogger(logger *slog.Logger) Option {
return func(cfg *config) error {
cfg.logger = logger
return nil
}
}