-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
393 lines (339 loc) · 11.3 KB
/
server.go
File metadata and controls
393 lines (339 loc) · 11.3 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
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
package napi
import (
"fmt"
"github.com/gofiber/fiber/v2/middleware/cache"
"github.com/gofiber/fiber/v2/utils"
"io"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/compress"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/etag"
"github.com/gofiber/fiber/v2/middleware/favicon"
"github.com/gofiber/fiber/v2/middleware/limiter"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/pprof"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/gofiber/fiber/v2/middleware/requestid"
"github.com/netr/napi/middleware"
)
// Server fiber app instance
type Server struct {
app *fiber.App
catchAll bool
port int
}
// ServerOption type used for option pattern
type ServerOption func(*Server)
// NewServer Creates a new fiber instance with a fiber.Config and stackable options using the options pattern. DefaultFiberConfig() should be used in most cases.
func NewServer(fiberCfg fiber.Config, opts ...ServerOption) *Server {
app := fiber.New(fiberCfg)
s := &Server{
app: app,
port: 1337,
}
for _, opt := range opts {
opt(s)
}
return s
}
// WithBaseMiddlewares a set of middlewares that are essentially plug and play. If these base middlewares need more flexibility, we can create individual options for them as we need them.
func WithBaseMiddlewares() ServerOption {
return func(s *Server) {
s.UseBaseMiddlewares()
}
}
// WithPort set the web server port.
func WithPort(p int) ServerOption {
return func(s *Server) {
s.Port(p)
}
}
// WithCatchAll sets up a simple catch all handler. This has to be a bool and used when Run() is called. If you set the catch all handler before the routes created by the application, everything will be caught. The bool removes this problem.
func WithCatchAll() ServerOption {
return func(s *Server) {
s.catchAll = true
}
}
// WithPrometheus adds the middleware for prometheus.
func WithPrometheus(serviceName ...string) ServerOption {
return func(s *Server) {
s.UsePrometheus(serviceName...)
}
}
// WithPprof adds the middleware for running pprof. This prefix will be added to the default path of "/debug/pprof/", for a resulting URL of: "/#endpoint#/debug/pprof/".
func WithPprof(endpoint ...string) ServerOption {
return func(s *Server) {
s.UsePprof(endpoint...)
}
}
// WithLogger use the logger middleware with a custom logger.Config struct. Use this when you need full control of the logger. The other logger helpers are designed to be called on their own.
func WithLogger(cfg logger.Config) ServerOption {
return func(s *Server) {
s.UseLogger(cfg)
}
}
// WithDefaultLogger use the logger middleware with the default configuration.
func WithDefaultLogger() ServerOption {
return func(s *Server) {
s.UseDefaultLogger()
}
}
// WithLoggerOutput use the logger middleware with a custom output writer. Can use os.Stdout, os.File, bytes.Buffer, etc. This uses the default logger config. Meant to be used by itself.
func WithLoggerOutput(w io.Writer) ServerOption {
return func(s *Server) {
s.UseLoggerOutput(w)
}
}
// WithLoggerDoneCallback use the logger middleware with a custom done callback after a log is written.
func WithLoggerDoneCallback(cb func(c *fiber.Ctx, logString []byte)) ServerOption {
return func(s *Server) {
s.UseLoggerDoneCallback(cb)
}
}
// WithLimiter use the Limiter middleware with the a custom configuration.
func WithLimiter(cfg limiter.Config) ServerOption {
return func(s *Server) {
s.app.Use(limiter.New(cfg))
}
}
// WithDefaultLimiter use the Limiter middleware with the default configuration.
func WithDefaultLimiter() ServerOption {
return func(s *Server) {
s.app.Use(limiter.New())
}
}
// DefaultFiberConfig basic fiber configuration with write and read timeouts set under the hood to 30 seconds. Can expand this but might as well just create your own fiber.Config.
func DefaultFiberConfig(appName string) fiber.Config {
return fiber.Config{
AppName: appName,
ReadTimeout: time.Second * 30,
WriteTimeout: time.Second * 30,
}
}
// WithCORS use the CORS middleware with the a custom configuration.
func WithCORS(cfg cors.Config) ServerOption {
return func(s *Server) {
s.UseCORS(cfg)
}
}
// WithDefaultCORS use the CORS middleware with the default configuration.
func WithDefaultCORS() ServerOption {
return func(s *Server) {
s.UseDefaultLogger()
}
}
// WithHealth opens up a health ping endpoint to be used for uptime monitoring.
func WithHealth() ServerOption {
return func(s *Server) {
s.UseHealth()
}
}
// WithCache uses cache control headers to set the cache control header to public and max age to 1 year.
func WithCache(cfg cache.Config) ServerOption {
return func(s *Server) {
s.UseCache(cfg)
}
}
// WithDefaultCache uses cache control headers to set the cache control header to public and max age to 1 year.
func WithDefaultCache() ServerOption {
return func(s *Server) {
s.UseDefaultCache()
}
}
// Run start the fiber server. Should always be called instead of s.App().Listen(). Uses a graceful shutdown mechanism from https://github.com/gofiber/recipes/blob/7a04f52833b70b97251d8a37893d2e0c599a8c15/graceful-shutdown/main.go
//
// Catch all needs to be called here to not interfere with routing being created by the application.
func (s *Server) Run() {
if s.catchAll {
if !s.pathExists("*") {
s.CatchAll()
}
}
go func() {
if err := s.app.Listen(fmt.Sprintf(":%d", s.port)); err != nil {
log.Panic(err)
}
}()
c := make(chan os.Signal, 1) // Store channel to signify a signal being sent
signal.Notify(c, os.Interrupt, syscall.SIGTERM) // When an interrupt or termination signal is sent, notify the channel
<-c // This blocks the main thread until an interrupt is received
log.Println("Gracefully shutting down...")
_ = s.app.Shutdown()
log.Println("Running cleanup tasks...")
}
// CatchAll helper function to automatically catch bad urls
func (s *Server) CatchAll() fiber.Router {
return s.app.All("*", func(c *fiber.Ctx) error {
_ = c.SendStatus(http.StatusNotFound)
return c.JSON(&fiber.Map{
"message": fmt.Sprintf("route '%s' not found", c.OriginalURL()),
"error": "endpoint not found",
})
})
}
// App returns the underlying fiber app instance
func (s *Server) App() *fiber.App {
return s.app
}
// UseLogger use the logger middleware with a custom logger.Config struct. Use this when you need full control of the logger. The other logger helpers are designed to be called on their own.
func (s *Server) UseLogger(cfg logger.Config) *Server {
s.app.Use(logger.New(cfg))
return s
}
// UseDefaultLogger use the logger middleware with the default configuration.
func (s *Server) UseDefaultLogger() *Server {
s.app.Use(logger.New())
return s
}
// UseLoggerOutput use the logger middleware with a custom output writer. Can use os.Stdout, os.File, bytes.Buffer, etc. This uses the default logger config. Meant to be used by itself.
func (s *Server) UseLoggerOutput(w io.Writer) *Server {
cfg := defaultLoggerConfig()
cfg.Output = w
s.app.Use(logger.New(cfg))
return s
}
// UseLoggerDoneCallback use the logger middleware with a custom done callback after a log is written.
func (s *Server) UseLoggerDoneCallback(cb func(c *fiber.Ctx, logString []byte)) *Server {
cfg := defaultLoggerConfig()
cfg.Done = cb
s.app.Use(logger.New(cfg))
return s
}
// UseBaseMiddlewares a set of middlewares that are essentially plug and play. If these base middlewares need more flexibility, we can create individual options for them as we need them.
func (s *Server) UseBaseMiddlewares() *Server {
useBaseMiddlewares(s.app)
return s
}
// Port helper function to set web server port.
func (s *Server) Port(p int) *Server {
s.port = p
return s
}
// UsePrometheus helper function to set prometheus middleware.
func (s *Server) UsePrometheus(serviceName ...string) *Server {
sn := ToSnakeCase(s.app.Config().AppName)
if len(serviceName) > 1 {
sn = serviceName[0]
}
prometheus := middleware.NewPrometheus(sn)
prometheus.RegisterAt(s.app, "/metrics")
s.app.Use(prometheus.Middleware)
return s
}
// UsePprof adds the middleware for running pprof. This prefix will be added to the default path of "/debug/pprof/", for a resulting URL of: "/#endpoint#/debug/pprof/".
func (s *Server) UsePprof(endpoint ...string) *Server {
cfg := pprof.Config{Next: nil}
if len(endpoint) > 0 {
cfg.Prefix = endpoint[0]
}
s.app.Use(pprof.New(cfg))
return s
}
// UseCORS use the CORS middleware with the a custom configuration.
func (s *Server) UseCORS(cfg cors.Config) *Server {
s.app.Use(cors.New(cfg))
return s
}
// UseDefaultCORS use the CORS middleware with the default configuration.
func (s *Server) UseDefaultCORS() *Server {
s.app.Use(cors.New())
return s
}
// UseLimiter use the Limiter middleware with the a custom configuration.
func (s *Server) UseLimiter(cfg limiter.Config) *Server {
s.app.Use(limiter.New(cfg))
return s
}
// UseDefaultLimiter use the Limiter middleware with the default configuration.
func (s *Server) UseDefaultLimiter() *Server {
s.app.Use(limiter.New())
return s
}
// UseHealth opens up a health ping endpoint to be used for uptime monitoring.
func (s *Server) UseHealth() *Server {
s.app.Get("/health", func(c *fiber.Ctx) error {
_ = c.SendStatus(http.StatusOK)
return c.JSON(&fiber.Map{
"message": "OK",
})
})
return s
}
// UseCache uses cache control headers to set the cache control header to public and max age to 1 year.
func (s *Server) UseCache(cfg cache.Config) *Server {
s.app.Use(cache.New(cfg))
return s
}
// UseDefaultCache uses cache control headers to set the cache control header to public and max age to 1 year.
func (s *Server) UseDefaultCache() *Server {
s.app.Use(cache.New(defaultCacheConfig()))
return s
}
// pathExists scans the app route stack for a matching path.
func (s *Server) pathExists(path string) bool {
found := false
for _, routes := range s.app.Stack() {
for _, route := range routes {
if route.Path == path {
found = true
break
}
}
}
return found
}
// methodAndPathExists scans the app route stack for a matching method and path.
func (s *Server) methodAndPathExists(method, path string) bool {
found := false
for _, routes := range s.app.Stack() {
for _, route := range routes {
if route.Path == path && route.Method == method {
found = true
break
}
}
}
return found
}
// useBaseMiddlewares plug and play middlewares for general use.
func useBaseMiddlewares(app *fiber.App) {
app.Use(compress.New())
app.Use(etag.New())
app.Use(favicon.New())
app.Use(requestid.New())
app.Use(recover.New())
}
// defaultLoggerConfig returns the default logger from the fiber docs.
func defaultLoggerConfig() logger.Config {
return logger.Config{
Next: nil,
Done: nil,
Format: "[${time}] ${status} - ${latency} ${method} ${path}\n",
TimeFormat: "15:04:05",
TimeZone: "Local",
TimeInterval: 500 * time.Millisecond,
Output: os.Stdout,
}
}
// defaultCacheConfig is the default config
func defaultCacheConfig() cache.Config {
return cache.Config{
Next: nil,
Expiration: 1 * time.Minute,
CacheControl: false,
KeyGenerator: func(c *fiber.Ctx) string {
return utils.CopyString(c.Path())
},
ExpirationGenerator: nil,
StoreResponseHeaders: false,
Storage: nil,
MaxBytes: 0,
Methods: []string{fiber.MethodGet, fiber.MethodHead},
}
}