-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathcontext.go
34 lines (28 loc) · 920 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
32
33
34
package session
import (
"context"
"net/http"
)
// Define the keys in the context
type (
ctxResKey struct{}
ctxReqKey struct{}
)
// returns a new Context that carries value res.
func newResContext(ctx context.Context, res http.ResponseWriter) context.Context {
return context.WithValue(ctx, ctxResKey{}, res)
}
// FromResContext returns the ResponseWriter value stored in ctx, if any.
func FromResContext(ctx context.Context) (http.ResponseWriter, bool) {
res, ok := ctx.Value(ctxResKey{}).(http.ResponseWriter)
return res, ok
}
// returns a new Context that carries value req.
func newReqContext(ctx context.Context, req *http.Request) context.Context {
return context.WithValue(ctx, ctxReqKey{}, req)
}
// FromReqContext returns the Request value stored in ctx, if any.
func FromReqContext(ctx context.Context) (*http.Request, bool) {
req, ok := ctx.Value(ctxReqKey{}).(*http.Request)
return req, ok
}