/
middleware.go
67 lines (61 loc) · 1.88 KB
/
middleware.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
61
62
63
64
65
66
67
package handler
import (
"errors"
"net/http"
"time"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/render"
"go.uber.org/zap"
)
// Logger is a middleware that logs the start and end of each request, along
// with some useful data about what was requested, what the response status was,
// and how long it took to return.
func (h *Handler) Logger() func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
t1 := time.Now()
defer func() {
h.log.Info("Served",
zap.String("proto", r.Proto),
zap.String("path", r.URL.Path),
zap.Duration("lat", time.Since(t1)),
zap.Int("status", ww.Status()),
zap.Int("size", ww.BytesWritten()),
zap.String("reqId", middleware.GetReqID(r.Context())))
}()
next.ServeHTTP(ww, r)
}
return http.HandlerFunc(fn)
}
}
// Recoverer is a middleware for recovering after panics during handling http requests
func (h *Handler) Recoverer(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rvr := recover(); rvr != nil {
if rvr == http.ErrAbortHandler {
// we don't recover http.ErrAbortHandler so the response
// to the client is aborted, this should not be logged
panic(rvr)
}
if err := render.Render(w, r, ErrInternalServer(errors.New("something went wrong"))); err != nil {
h.log.Error(err.Error())
}
}
}()
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
// SendRequestID is a middleware for sending X-Request-Id to the client
func SendRequestID(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
w.Header().Add(
middleware.RequestIDHeader,
middleware.GetReqID(r.Context()),
)
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}