-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
37 lines (34 loc) · 1.07 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
package gateway
import (
"context"
"log"
"net/http"
)
// tracing is a mw that wraps a handler, providing it with a requestID embeded in its context.
func tracing(nextRequestID func() string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestID := r.Header.Get("X-Request-ID")
if requestID == "" {
requestID = nextRequestID()
}
ctx := context.WithValue(context.Background(), requestIDKey, requestID)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}
// logging is a mw that wraps a handler enabling it logging on a route-basis.
func logging(logger *log.Logger) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
requestID, ok := r.Context().Value(requestIDKey).(string)
if !ok {
requestID = "unknown"
}
logger.Println(requestID, r.Method, r.URL.Path, r.RemoteAddr, r.UserAgent())
}()
next.ServeHTTP(w, r)
})
}
}