-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
61 lines (45 loc) · 1.25 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
package middleware
import (
"context"
"net/http"
"github.com/rs/xid"
"github.com/rs/zerolog"
)
func RequestLogger(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
logger := zerolog.Ctx(r.Context())
logger.UpdateContext(func(c zerolog.Context) zerolog.Context {
c = c.Str("url_path", r.URL.Path)
c = c.Str("http_method", r.Method)
c = c.Str("ip", r.RemoteAddr)
c = c.Str("user_agent", r.UserAgent())
return c
})
next.ServeHTTP(w, r)
logger.Info().Msgf("%s %s", r.Method, r.URL.String())
}
return http.HandlerFunc(fn)
}
func requestID(header string, r *http.Request) string {
id := r.Header.Get(header)
if id == "" {
id = xid.New().String()
}
return id
}
func RequestID(fieldKey, headerName string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logger := zerolog.Ctx(ctx)
id := requestID(headerName, r)
ctx = context.WithValue(ctx, "requestID", id)
r = r.WithContext(ctx)
logger.UpdateContext(func(c zerolog.Context) zerolog.Context {
return c.Str(fieldKey, id)
})
w.Header().Set(headerName, id)
next.ServeHTTP(w, r)
})
}
}