1
|
|
|
package http |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"time" |
5
|
|
|
|
6
|
|
|
"github.com/ansrivas/fiberprometheus/v2" |
7
|
|
|
"github.com/gofiber/contrib/fibernewrelic" |
8
|
|
|
"github.com/gofiber/fiber/v2" |
9
|
|
|
"github.com/gofiber/fiber/v2/middleware/cache" |
10
|
|
|
"github.com/gofiber/fiber/v2/middleware/compress" |
11
|
|
|
"github.com/gofiber/fiber/v2/middleware/cors" |
12
|
|
|
"github.com/gofiber/fiber/v2/middleware/logger" |
13
|
|
|
"github.com/gofiber/swagger" |
14
|
|
|
"github.com/memnix/memnix-rest/app/misc" |
15
|
|
|
"github.com/memnix/memnix-rest/config" |
16
|
|
|
_ "github.com/memnix/memnix-rest/docs" // Side effect import |
17
|
|
|
"github.com/memnix/memnix-rest/infrastructures" |
18
|
|
|
) |
19
|
|
|
|
20
|
|
|
// New returns a new Fiber instance |
21
|
|
|
func New() *fiber.App { |
22
|
|
|
// Create new app |
23
|
|
|
|
24
|
|
|
app := fiber.New( |
25
|
|
|
fiber.Config{ |
26
|
|
|
Prefork: false, |
27
|
|
|
JSONDecoder: config.JSONHelper.Unmarshal, |
28
|
|
|
JSONEncoder: config.JSONHelper.Marshal, |
29
|
|
|
}) |
30
|
|
|
|
31
|
|
|
// Register middlewares |
32
|
|
|
registerMiddlewares(app) |
33
|
|
|
|
34
|
|
|
app.Get("/", func(c *fiber.Ctx) error { |
35
|
|
|
return c.SendString("Hello, World 👋!") |
36
|
|
|
}) |
37
|
|
|
|
38
|
|
|
// Use swagger middleware |
39
|
|
|
app.Get("/swagger/*", swagger.HandlerDefault) // default |
40
|
|
|
|
41
|
|
|
// Api group |
42
|
|
|
v2 := app.Group("/v2") |
43
|
|
|
|
44
|
|
|
v2.Get("/", func(c *fiber.Ctx) error { |
45
|
|
|
return fiber.NewError(fiber.StatusForbidden, "This is not a valid route") // Custom error |
46
|
|
|
}) |
47
|
|
|
|
48
|
|
|
registerRoutes(&v2) // /v2 |
49
|
|
|
|
50
|
|
|
return app |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
func registerMiddlewares(app *fiber.App) { |
54
|
|
|
// Use cors middleware |
55
|
|
|
app.Use(cors.New(cors.Config{ |
56
|
|
|
AllowOrigins: "http://localhost, *", |
57
|
|
|
AllowHeaders: "Origin, Content-Type, Accept, Authorization, Cache-Control", |
58
|
|
|
AllowCredentials: true, |
59
|
|
|
})) |
60
|
|
|
|
61
|
|
|
app.Use(cache.New(cache.Config{ |
62
|
|
|
Expiration: 5 * time.Second, |
63
|
|
|
CacheControl: true, |
64
|
|
|
Next: func(c *fiber.Ctx) bool { |
65
|
|
|
// Do not cache /metrics endpoint |
66
|
|
|
return c.Path() == "/metrics" |
67
|
|
|
}, |
68
|
|
|
})) |
69
|
|
|
|
70
|
|
|
// Use compress middleware |
71
|
|
|
app.Use(compress.New(compress.Config{ |
72
|
|
|
Level: compress.LevelBestSpeed, // 1 |
73
|
|
|
})) |
74
|
|
|
|
75
|
|
|
cfg := fibernewrelic.Config{ |
76
|
|
|
Application: infrastructures.GetRelicApp(), |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
app.Use(fibernewrelic.New(cfg)) |
80
|
|
|
|
81
|
|
|
// User logging middleware |
82
|
|
|
app.Use(logger.New(logger.Config{ |
83
|
|
|
Format: "[${time}] - [${ip}]:${port} - ${latency} ${method} ${path} - ${status}\n", |
84
|
|
|
TimeFormat: "Jan 02 | 15:04:05", |
85
|
|
|
Output: misc.LogWriter{}, |
86
|
|
|
})) |
87
|
|
|
|
88
|
|
|
prometheus := fiberprometheus.New("memnix") |
89
|
|
|
prometheus.RegisterAt(app, "/metrics") |
90
|
|
|
app.Use(prometheus.Middleware) |
91
|
|
|
} |
92
|
|
|
|