-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddlewares.go
43 lines (36 loc) · 1.08 KB
/
middlewares.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
package toolbox
import (
"context"
"errors"
"net/http"
)
type key string
const (
reqContextMethod key = "toolbox_req_context_method"
reqContextPath key = "toolbox_req_context_path"
)
// GetRequestContext returns a logger from a context.
func GetRequestContext(ctx context.Context) (method, path string, err error) {
method, ok := ctx.Value(reqContextMethod).(string)
if !ok {
return "", "", errors.New("request method not found")
}
path, ok = ctx.Value(reqContextPath).(string)
if !ok {
return "", "", errors.New("request path not found")
}
return method, path, nil
}
type requestContext struct{}
// NewRequestContext returns a new RequestContext middleware.
func NewRequestContext() func(next http.Handler) http.Handler {
l := &requestContext{}
return l.middleware
}
func (r *requestContext) middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), reqContextMethod, r.Method)
ctx = context.WithValue(ctx, reqContextPath, r.URL.Path)
next.ServeHTTP(w, r.WithContext(ctx))
})
}