-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
64 lines (54 loc) · 1.76 KB
/
request.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
package tea
import (
"context"
"encoding/json"
"net/http"
"github.com/apex/log"
"github.com/go-chi/chi/middleware"
)
// Body parses a JSON request body and validates it's contents
func Body(r *http.Request, v interface{}) error {
if err := json.NewDecoder(r.Body).Decode(v); err != nil {
return err
}
return Validate.StructCtx(r.Context(), v)
}
type key int
const loggerKey key = 1
// NewLogger creates a new logger from an *http.Request. It is used
// to create a new log.Interface for middleware and when an existing
// logger does not exist. It is exposed so that it can be overridden
//
// The default NewLogger creates a new logger with the request.id
// from middleware.GetReqID, and the client.ip from r.RemoteAddr
var NewLogger = func(r *http.Request) log.Interface {
return log.WithFields(log.Fields{
"request.id": middleware.GetReqID(r.Context()),
"client.ip": r.RemoteAddr,
})
}
// Logger pulls a logger from request context or creates a new one
// with NewLogger if one does not exist
func Logger(r *http.Request) log.Interface {
ctx := r.Context()
if ll, ok := ctx.Value(loggerKey).(log.Interface); ok {
return ll
}
return NewLogger(r)
}
// LoggerCtx uses Logger to get a log.Interface and appends it to
// the request context, returning the new context with the logger value
// to be retrieved within Handlers
func LoggerCtx(r *http.Request) (log.Interface, context.Context) {
l := Logger(r)
return l, context.WithValue(r.Context(), loggerKey, l)
}
// LoggerMiddleware appends a logger with request information using
// LoggerCtx to every request
func LoggerMiddleware(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
_, ctx := LoggerCtx(r)
next.ServeHTTP(w, r.WithContext(ctx))
}
return http.HandlerFunc(fn)
}