-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
55 lines (42 loc) · 1002 Bytes
/
router.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
package main
import (
"net/http"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"go.uber.org/fx"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type RouterParams struct {
fx.In
Handlers []Handler `group:"handlers"`
Registry Registry
}
type RouterResult struct {
fx.Out
Router *chi.Mux
}
func NewRouter(p RouterParams) (RouterResult, error) {
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Page not found", http.StatusNotFound)
})
r.Handle(
"/metrics",
promhttp.HandlerFor(
p.Registry.Registry,
promhttp.HandlerOpts{Registry: p.Registry.Registry},
),
)
for _, handler := range p.Handlers {
if handler.Type() == "GET" {
r.Get(handler.Pattern(), handler.ServeHTTP)
}
if handler.Type() == "POST" {
r.Post(handler.Pattern(), handler.ServeHTTP)
}
}
return RouterResult{Router: r}, nil
}