-
Notifications
You must be signed in to change notification settings - Fork 3
/
sessionid.go
51 lines (44 loc) · 1.11 KB
/
sessionid.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
package roundtripware
import (
"net/http"
"go.uber.org/zap"
keelhttpcontext "github.com/foomo/keel/net/http/context"
)
type (
SessionIDOptions struct {
Header string
}
SessionIDOption func(*SessionIDOptions)
SessionIDGenerator func() string
)
// GetDefaultSessionIDOptions returns the default options
func GetDefaultSessionIDOptions() SessionIDOptions {
return SessionIDOptions{
Header: "X-Session-ID",
}
}
// SessionIDWithHeader middleware option
func SessionIDWithHeader(v string) SessionIDOption {
return func(o *SessionIDOptions) {
o.Header = v
}
}
// SessionID returns a RoundTripper which prints out the request & response object
func SessionID(opts ...SessionIDOption) RoundTripware {
o := GetDefaultSessionIDOptions()
for _, opt := range opts {
if opt != nil {
opt(&o)
}
}
return func(l *zap.Logger, next Handler) Handler {
return func(r *http.Request) (*http.Response, error) {
if value := r.Header.Get(o.Header); value == "" {
if value, ok := keelhttpcontext.GetSessionID(r.Context()); ok && value != "" {
r.Header.Set(o.Header, value)
}
}
return next(r)
}
}
}