-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathcontext.go
31 lines (28 loc) Β· 954 Bytes
/
context.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
package middleware
import (
"context"
"net/http"
)
// RequestContext returns a middleware which initializes the request context.
func RequestContext(ctx context.Context) func(http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(w, r.WithContext(ctx))
})
}
}
// RequestContextKeyVals returns a middleware which adds the given key/value pairs to the
// request context.
func RequestContextKeyVals(keyvals ...interface{}) func(http.Handler) http.Handler {
if len(keyvals)%2 != 0 {
panic("initctx: invalid number of key/value elements, must be an even number")
}
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for i := 0; i < len(keyvals); i += 2 {
r = r.WithContext(context.WithValue(r.Context(), keyvals[i], keyvals[i+1]))
}
h.ServeHTTP(w, r)
})
}
}