-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoptions.go
318 lines (281 loc) · 8.3 KB
/
options.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
package webfmwk
import (
"context"
"log/slog"
"net/http"
"sync"
"time"
"github.com/valyala/fasthttp"
)
type (
// Option apply specific configuration to the server at init time
// They are tu be used this way :
// s := w.InitServer(
// webfmwk.WithStructuredLogger(slog.Default()),
// webfmwk.WithCtrlC(),
// webfmwk.CheckIsUp(),
// webfmwk.WithCORS(),
// webfmwk.SetPrefix("/api"),
// webfmwk.WithDocHanlders(redoc.GetHandler()),
// webfmwk.SetIdleTimeout(1 * time.Second),
// webfmwk.SetReadTimeout(1 * time.Second),
// webfmwk.SetWriteTimeout(1 * time.Second),
// webfmwk.WithHanlders(
// recover.Handler,
// logging.Handler,
// security.Handler))
Option func(s *Server)
// Options is a list of options
Options []Option
serverMeta struct {
socketIOHandler http.Handler
socketIOHandlerFunc http.HandlerFunc
baseServer *fasthttp.Server
routes RoutesPerPrefix
prefix string
pprofPath string
socketIOPath string
docHandlers []DocHandler
handlers []Handler
cors bool
socketIOHF bool
socketIOH bool
pprof bool
enableKeepAlive bool
ctrlcStarted bool
checkIsUp bool
ctrlc bool
http2 bool
}
)
var once sync.Once
func initOnce() error { return initValidator() }
// UseOption apply the param o option to the params s server
func UseOption(s *Server, o Option) {
o(s)
}
// useOptions apply the params opts option to the param s server
func useOptions(s *Server, opts ...Option) {
for _, o := range opts {
UseOption(s, o)
}
}
// InitServer initialize a webfmwk.Server instance.
// It may take some server options as parameters.
// List of server options : WithLogger, WithCtrlC, CheckIsUp, WithCORS, SetPrefix,
// WithHandlers, WithDocHandler, SetReadTimeout, SetWriteTimeout, SetIdleTimeout,
// EnableKeepAlive.
// Any error returned by the method should be handled as a fatal one.
func InitServer(opts ...Option) (*Server, error) {
var e error
once.Do(func() { e = initOnce() })
var (
wg sync.WaitGroup
ctx, cancel = context.WithCancel(context.Background())
s = &Server{
launcher: CreateWorkerLauncher(&wg, cancel),
ctx: ctx,
cancel: cancel,
wg: &wg,
slog: slog.Default(),
isReady: make(chan bool),
meta: getDefaultMeta(),
}
)
useOptions(s, opts...)
return s, e
}
// WithHTTP2 enable HTTP2 capabilities.
func WithHTTP2() Option {
return func(s *Server) {
s.meta.http2 = true
s.slog.Debug("\t-- http2 enabled")
}
}
// WithStructuredLogger set the server structured logger which derive from slog.Logger.
// Try to set it the earliest possible.
func WithStructuredLogger(slg *slog.Logger) Option {
return func(s *Server) {
s.registerStructuredLogger(slg)
slg.Debug("logger loaded", "webfmwk", "init:option")
}
}
// WithCtrlC enable the internal ctrl+c support from the server.
func WithCtrlC() Option {
return func(s *Server) {
s.enableCtrlC()
s.slog.Debug("\t-- crtl-c support enabled")
}
}
// CheckIsUp expose a `/ping` endpoint and try to poll to check the server healt
// when it's started.
func CheckIsUp() Option {
return func(s *Server) {
s.EnableCheckIsUp()
s.slog.Debug("\t-- check is up support enabled")
}
}
// WithCORS enable the CORS (Cross-Origin Resource Sharing) support.
func WithCORS() Option {
return func(s *Server) {
s.enableCORS()
s.slog.Debug("\t-- CORS support enabled")
}
}
// SetPrefix set the API root prefix.
func SetPrefix(prefix string) Option {
return func(s *Server) {
s.setPrefix(prefix)
s.slog.Debug("\t-- api prefix loaded")
}
}
// WithDocHandlers allow to register custom DocHandler struct (ex: swaggo, redoc).
// If use with SetPrefix, register WithDocHandler after the SetPrefix one.
// Example:
//
// package main
//
// import (
// "github.com/burgesQ/webfmwk/v6"
// "github.com/burgesQ/webfmwk/v6/handler/redoc"
// )
//
// func main() {
// var s = webfmwk.InitServer(webfmwk.WithDocHandlers(redoc.GetHandler()))
// }
func WithDocHandlers(handler ...DocHandler) Option {
return func(s *Server) {
s.addDocHandlers(handler...)
s.slog.Debug("\t-- doc handlers loaded")
}
}
// WithHandlers allow to register a list of webfmwk.Handler
// Handler signature is the webfmwk.HandlerFunc one (func(c Context)).
// To register a custom context, simply do it in the toppest handler.
//
// package main
//
// import (
// "github.com/burgesQ/webfmwk/v6"
// "github.com/burgesQ/webfmwk/v6/handler/security"
// )
//
// type CustomContext struct {
// webfmwk.Context
// val String
// }
//
// func main() {
// var s = webfmwk.InitServer(webfmwk.WithHandlers(security.Handler,
// func(next Habdler) Handler {
// return func(c webfmwk.Context) error {
// cc := Context{c, "val"}
// return next(cc)
// }}))
func WithHandlers(h ...Handler) Option {
return func(s *Server) {
s.addHandlers(h...)
s.slog.Debug("\t-- handlers loaded")
}
}
// SetReadTimeout is a timing constraint on the client http request imposed by
// the server from the moment
// of initial connection up to the time the entire request body has been read.
//
// [Accept] --> [TLS Handshake] --> [Request Headers] --> [Request Body] --> [Response]
func SetReadTimeout(val time.Duration) Option {
return func(s *Server) {
s.meta.baseServer.ReadTimeout = val
s.slog.Debug("\t-- read timeout loaded")
}
}
// SetWriteTimeout is a time limit imposed on client connecting to the server
// via http from the
// time the server has completed reading the request header up to the time
// it has finished writing the response.
//
// [Accept] --> [TLS Handshake] --> [Request Headers] --> [Request Body] --> [Response]
func SetWriteTimeout(val time.Duration) Option {
return func(s *Server) {
s.meta.baseServer.WriteTimeout = val
s.slog.Debug("\t-- write timeout loaded")
}
}
// SetIDLETimeout the server IDLE timeout AKA keepalive timeout.
func SetIDLETimeout(val time.Duration) Option {
return func(s *Server) {
s.meta.baseServer.IdleTimeout = val
s.slog.Debug("\t-- idle aka keepalive timeout loaded")
}
}
// EnableKeepAlive disable the server keep alive functions.
func EnableKeepAlive() Option {
return func(s *Server) {
s.meta.enableKeepAlive = true
s.slog.Debug("\t-- keepalive disabled")
}
}
// EnablePprof enable the pprof endpoints.
func EnablePprof(path ...string) Option {
return func(s *Server) {
s.meta.pprof = true
if len(path) > 0 {
s.meta.pprofPath = path[0]
}
s.slog.Debug("\t-- pprof endpoint enabled")
}
}
func MaxRequestBodySize(size int) Option {
return func(s *Server) {
s.meta.baseServer.MaxRequestBodySize = size
s.slog.Debug("\t-- request max body size set", "value", size)
}
}
const (
ReadTimeout = 20
WriteTimeout = 20
IdleTimeout = 1
)
func WithSocketHandlerFunc(path string, hf http.HandlerFunc) Option {
return func(s *Server) {
s.meta.socketIOHandlerFunc, s.meta.socketIOPath, s.meta.socketIOHF = hf, path, true
s.slog.Debug("\t-- socket io handler func loaded")
}
}
func WithSocketHandler(path string, h http.Handler) Option {
return func(s *Server) {
s.meta.socketIOHandler, s.meta.socketIOPath, s.meta.socketIOH = h, path, true
s.slog.Debug("\t-- socket io handlers loaded")
}
}
// return default cfg
func getDefaultMeta() serverMeta {
return serverMeta{
baseServer: &fasthttp.Server{
ReadTimeout: ReadTimeout * time.Second,
WriteTimeout: WriteTimeout * time.Second,
IdleTimeout: IdleTimeout * time.Minute,
MaxRequestBodySize: fasthttp.DefaultMaxRequestBodySize,
},
routes: make(RoutesPerPrefix),
pprofPath: "/debug/pprof/{profile:*}",
}
}
func (m *serverMeta) toServer(addr string) *fasthttp.Server {
s := &fasthttp.Server{
ReadTimeout: m.baseServer.ReadTimeout,
WriteTimeout: m.baseServer.WriteTimeout,
IdleTimeout: m.baseServer.IdleTimeout,
MaxRequestBodySize: m.baseServer.MaxRequestBodySize,
Name: "webfmwk " + addr,
DisableKeepalive: !m.enableKeepAlive,
DisableHeaderNamesNormalizing: true,
ReduceMemoryUsage: true,
LogAllErrors: true,
CloseOnShutdown: true,
}
// if m.http2 {
// http2.ConfigureServer(s, http2.ServerConfig{})
// }
return s
}