-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathrequestid.go
46 lines (41 loc) Β· 1.53 KB
/
requestid.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
package middleware
import (
"context"
"net/http"
"goa.design/goa/middleware"
)
// RequestID returns a middleware, which initializes the context with a unique
// value under the RequestIDKey key. Optionally uses the incoming "X-Request-Id"
// header, if present, with or without a length limit to use as request ID. the
// default behavior is to always generate a new ID.
//
// examples of use:
// service.Use(middleware.RequestID())
//
// // enable options for using "X-Request-Id" header with length limit.
// service.Use(middleware.RequestID(
// middleware.UseXRequestIDHeaderOption(true),
// middleware.XRequestHeaderLimitOption(128)))
func RequestID(options ...middleware.RequestIDOption) func(http.Handler) http.Handler {
o := middleware.NewRequestIDOptions(options...)
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if o.IsUseRequestID() {
if id := r.Header.Get("X-Request-Id"); id != "" {
ctx = context.WithValue(ctx, middleware.RequestIDKey, id)
}
}
ctx = middleware.GenerateRequestID(ctx, o)
h.ServeHTTP(w, r.WithContext(ctx))
})
}
}
// UseXRequestIDHeaderOption enables/disables using "X-Request-Id" header.
func UseXRequestIDHeaderOption(f bool) middleware.RequestIDOption {
return middleware.UseRequestIDOption(f)
}
// XRequestHeaderLimitOption sets the option for using "X-Request-Id" header.
func XRequestHeaderLimitOption(limit int) middleware.RequestIDOption {
return middleware.RequestIDLimitOption(limit)
}