-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
33 lines (28 loc) · 875 Bytes
/
middleware.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
package fullstory
import "net/http"
func CreateSessionCookieMiddleware(cookieName string) func(http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(wr http.ResponseWriter, req *http.Request) {
ctx := req.Context()
cookie, err := req.Cookie(cookieName)
if err != nil {
h.ServeHTTP(wr, req)
return
}
h.ServeHTTP(wr, req.WithContext(WithSessionID(ctx, cookie.Value)))
})
}
}
func CreateSessionHeaderMiddleware(headerName string) func(http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(wr http.ResponseWriter, req *http.Request) {
ctx := req.Context()
sessionID := req.Header.Get(headerName)
if len(sessionID) == 0 {
h.ServeHTTP(wr, req)
return
}
h.ServeHTTP(wr, req.WithContext(WithSessionID(ctx, sessionID)))
})
}
}