-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
45 lines (37 loc) · 1.09 KB
/
handler.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
package server
import (
"log/slog"
"net/http"
"github.com/gemyago/golang-backend-boilerplate/internal/api/http/middleware"
"github.com/gemyago/golang-backend-boilerplate/internal/api/http/routes"
sloghttp "github.com/samber/slog-http"
"go.uber.org/dig"
)
type RootHandlerDeps struct {
dig.In
RootLogger *slog.Logger
Groups []routes.MountFunc `group:"server"`
}
func NewRootHandler(deps RootHandlerDeps) http.Handler {
mux := http.NewServeMux()
for _, grp := range deps.Groups {
grp(mux)
}
// Router wire-up
chain := middleware.Chain(
middleware.NewTracingMiddleware(middleware.NewTracingMiddlewareCfg()),
sloghttp.NewWithConfig(deps.RootLogger, sloghttp.Config{
DefaultLevel: slog.LevelInfo,
ClientErrorLevel: slog.LevelWarn,
ServerErrorLevel: slog.LevelError,
WithUserAgent: true,
WithRequestID: false, // We handle it ourselves (tracing middleware)
WithRequestHeader: true,
WithResponseHeader: true,
WithSpanID: true,
WithTraceID: true,
}),
middleware.NewRecovererMiddleware(deps.RootLogger),
)
return chain(mux)
}