/
http.go
54 lines (45 loc) · 1.3 KB
/
http.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
package http
import (
"time"
"github.com/capcom6/go-infra-fx/http/jsonify"
"github.com/capcom6/go-infra-fx/http/statuscode"
"github.com/gofiber/contrib/fiberzap/v2"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/recover"
)
const (
ReadTimeout = 5 * time.Second
WriteTimeout = 5 * time.Second
IdleTimeout = 60 * time.Second
)
func New(params Params) (*fiber.App, error) {
config := configDefault(params.Config)
app := fiber.New(fiber.Config{
ReadTimeout: ReadTimeout,
WriteTimeout: WriteTimeout,
IdleTimeout: IdleTimeout,
DisableStartupMessage: true,
ErrorHandler: errorHandler,
ProxyHeader: "X-Forwarded-For",
EnableTrustedProxyCheck: len(config.Proxies) > 0,
TrustedProxies: config.Proxies,
})
app.Use(recover.New())
app.Use(fiberzap.New(fiberzap.Config{
SkipBody: func(c *fiber.Ctx) bool {
return c.Response().StatusCode() < 400
},
Logger: params.Logger,
Fields: []string{"latency", "status", "method", "url", "ip", "ua", "body", "error"},
}))
for _, handler := range params.RootHandlers {
handler.Register(app)
}
api := app.Group("/api")
api.Use(jsonify.New())
for _, handler := range params.ApiHandlers {
handler.Register(api)
}
app.Use(statuscode.New())
return app, nil
}