This repository has been archived by the owner on Jul 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_pre17.go
162 lines (146 loc) · 5.47 KB
/
handler_pre17.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
// +build !go1.7
package xlog
import (
"net"
"net/http"
"github.com/rs/xhandler"
"github.com/rs/xid"
"golang.org/x/net/context"
)
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
}
// 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
}
// 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(xhandler.HandlerC) xhandler.HandlerC {
if c.Output == nil {
c.Output = NewOutputChannel(NewConsoleOutput())
}
return func(next xhandler.HandlerC) xhandler.HandlerC {
return xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
l := New(c)
ctx = NewContext(ctx, l)
next.ServeHTTPC(ctx, 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 xhandler.HandlerC) xhandler.HandlerC {
return func(next xhandler.HandlerC) xhandler.HandlerC {
return xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
FromContext(ctx).SetField(name, r.URL.String())
next.ServeHTTPC(ctx, 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 xhandler.HandlerC) xhandler.HandlerC {
return func(next xhandler.HandlerC) xhandler.HandlerC {
return xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
FromContext(ctx).SetField(name, r.Method)
next.ServeHTTPC(ctx, 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 xhandler.HandlerC) xhandler.HandlerC {
return func(next xhandler.HandlerC) xhandler.HandlerC {
return xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
FromContext(ctx).SetField(name, r.Method+" "+r.URL.String())
next.ServeHTTPC(ctx, 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 xhandler.HandlerC) xhandler.HandlerC {
return func(next xhandler.HandlerC) xhandler.HandlerC {
return xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
if host, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
FromContext(ctx).SetField(name, host)
}
next.ServeHTTPC(ctx, 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 xhandler.HandlerC) xhandler.HandlerC {
return func(next xhandler.HandlerC) xhandler.HandlerC {
return xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
if ua := r.Header.Get("User-Agent"); ua != "" {
FromContext(ctx).SetField(name, ua)
}
next.ServeHTTPC(ctx, 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 xhandler.HandlerC) xhandler.HandlerC {
return func(next xhandler.HandlerC) xhandler.HandlerC {
return xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
if ref := r.Header.Get("Referer"); ref != "" {
FromContext(ctx).SetField(name, ref)
}
next.ServeHTTPC(ctx, 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 xhandler.HandlerC) xhandler.HandlerC {
return func(next xhandler.HandlerC) xhandler.HandlerC {
return xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
id, ok := IDFromContext(ctx)
if !ok {
id = xid.New()
ctx = context.WithValue(ctx, idKey, id)
}
if name != "" {
FromContext(ctx).SetField(name, id)
}
if headerName != "" {
w.Header().Set(headerName, id.String())
}
next.ServeHTTPC(ctx, w, r)
})
}
}