-
Notifications
You must be signed in to change notification settings - Fork 12
/
handler.go
189 lines (171 loc) · 5.66 KB
/
handler.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// +build go1.7
package xlog
import (
"context"
"net"
"net/http"
"github.com/rs/xid"
)
type key int
const (
logKey key = iota
idKey
)
// IDFromContext returns the unique id associated to the request if any.
func IDFromContext(ctx context.Context) (xid.ID, bool) {
id, ok := ctx.Value(idKey).(xid.ID)
return id, ok
}
// IDFromRequest returns the unique id accociated to the request if any.
func IDFromRequest(r *http.Request) (xid.ID, bool) {
if r == nil {
return xid.ID{}, false
}
return IDFromContext(r.Context())
}
// FromContext gets the logger out of the context.
// If not logger is stored in the context, a NopLogger is returned.
func FromContext(ctx context.Context) Logger {
if ctx == nil {
return NopLogger
}
l, ok := ctx.Value(logKey).(Logger)
if !ok {
return NopLogger
}
return l
}
// FromRequest gets the logger in the request's context.
// This is a shortcut for xlog.FromContext(r.Context())
func FromRequest(r *http.Request) Logger {
if r == nil {
return NopLogger
}
return FromContext(r.Context())
}
// NewContext returns a copy of the parent context and associates it with the provided logger.
func NewContext(ctx context.Context, l Logger) context.Context {
return context.WithValue(ctx, logKey, l)
}
// NewHandler instanciates a new xlog HTTP handler.
//
// If not configured, the output is set to NewConsoleOutput() by default.
func NewHandler(c Config) func(http.Handler) http.Handler {
if c.Output == nil {
c.Output = NewOutputChannel(NewConsoleOutput())
}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var l Logger
if r != nil {
l = New(c)
r = r.WithContext(NewContext(r.Context(), l))
}
next.ServeHTTP(w, r)
if l, ok := l.(*logger); ok {
l.close()
}
})
}
}
// URLHandler returns a handler setting the request's URL as a field
// to the current context's logger using the passed name as field name.
func URLHandler(name string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := FromContext(r.Context())
l.SetField(name, r.URL.String())
next.ServeHTTP(w, r)
})
}
}
// MethodHandler returns a handler setting the request's method as a field
// to the current context's logger using the passed name as field name.
func MethodHandler(name string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := FromContext(r.Context())
l.SetField(name, r.Method)
next.ServeHTTP(w, r)
})
}
}
// RequestHandler returns a handler setting the request's method and URL as a field
// to the current context's logger using the passed name as field name.
func RequestHandler(name string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := FromContext(r.Context())
l.SetField(name, r.Method+" "+r.URL.String())
next.ServeHTTP(w, r)
})
}
}
// RemoteAddrHandler returns a handler setting the request's remote address as a field
// to the current context's logger using the passed name as field name.
func RemoteAddrHandler(name string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if host, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
l := FromContext(r.Context())
l.SetField(name, host)
}
next.ServeHTTP(w, r)
})
}
}
// UserAgentHandler returns a handler setting the request's client's user-agent as
// a field to the current context's logger using the passed name as field name.
func UserAgentHandler(name string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if ua := r.Header.Get("User-Agent"); ua != "" {
l := FromContext(r.Context())
l.SetField(name, ua)
}
next.ServeHTTP(w, r)
})
}
}
// RefererHandler returns a handler setting the request's referer header as
// a field to the current context's logger using the passed name as field name.
func RefererHandler(name string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if ref := r.Header.Get("Referer"); ref != "" {
l := FromContext(r.Context())
l.SetField(name, ref)
}
next.ServeHTTP(w, r)
})
}
}
// RequestIDHandler returns a handler setting a unique id to the request which can
// be gathered using IDFromContext(ctx). This generated id is added as a field to the
// logger using the passed name as field name. The id is also added as a response
// header if the headerName is not empty.
//
// The generated id is a URL safe base64 encoded mongo object-id-like unique id.
// Mongo unique id generation algorithm has been selected as a trade-off between
// size and ease of use: UUID is less space efficient and snowflake requires machine
// configuration.
func RequestIDHandler(name, 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()
id, ok := IDFromContext(ctx)
if !ok {
id = xid.New()
ctx = context.WithValue(ctx, idKey, id)
r = r.WithContext(ctx)
}
if name != "" {
FromContext(ctx).SetField(name, id)
}
if headerName != "" {
w.Header().Set(headerName, id.String())
}
next.ServeHTTP(w, r)
})
}
}