/
router.go
60 lines (49 loc) · 1.48 KB
/
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
56
57
58
59
60
package http
import (
"fmt"
"net/http"
"time"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/rs/zerolog"
"github.com/rs/zerolog/hlog"
)
// Router represents a Todo's HTTP router, with the appropriate endpoints
// configured.
type Router struct {
mux *chi.Mux
}
// Endpoints specifies the Todo API endpoint configurations as
// a list of http.Handlers.
type Endpoints struct {
Healthcheck http.HandlerFunc
Todos http.HandlerFunc
}
// NewRouter provides REST endpoint routing for the Todo API
func NewRouter(endpoints Endpoints, log zerolog.Logger) *Router {
mux := chi.NewRouter()
mux.Use(hlog.NewHandler(log))
mux.Use(hlog.MethodHandler("http_method"))
mux.Use(hlog.URLHandler("url"))
mux.Use(hlog.RequestIDHandler("request_id", "Request-ID"))
mux.Use(hlog.AccessHandler(accessHandlerLogging))
mux.Use(middleware.Recoverer)
mux.Use(middleware.Heartbeat("/ping"))
mux.Get("/health", endpoints.Healthcheck)
mux.Get("/todos", endpoints.Todos)
return &Router{mux}
}
// ServeOn serves the router endpoints on the supplied port.
func (router *Router) ServeOn(port int) error {
return http.ListenAndServe(fmt.Sprintf(":%d", port), router.mux)
}
// Serve a HTTP request
func (router *Router) Serve(w http.ResponseWriter, r *http.Request) {
router.mux.ServeHTTP(w, r)
}
func accessHandlerLogging(r *http.Request, status, size int, duration time.Duration) {
hlog.FromRequest(r).Info().
Int("http_status", status).
Int("response_size", size).
Send()
}