-
Notifications
You must be signed in to change notification settings - Fork 27
/
context.go
43 lines (36 loc) · 993 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
35
36
37
38
39
40
41
42
43
package live
import (
"context"
"net/http"
)
type contextKey string
const (
requestKey contextKey = "context_request"
writerKey contextKey = "context_writer"
)
// contextWithRequest embed the initiating request within the context.
func contextWithRequest(ctx context.Context, r *http.Request) context.Context {
return context.WithValue(ctx, requestKey, r)
}
// Request pulls out an initiating request from a context.
func Request(ctx context.Context) *http.Request {
data := ctx.Value(requestKey)
r, ok := data.(*http.Request)
if !ok {
return nil
}
return r
}
// contextWithWriter embed the response writer within the context.
func contextWithWriter(ctx context.Context, w http.ResponseWriter) context.Context {
return context.WithValue(ctx, writerKey, w)
}
// Writer pulls out a response writer from a context.
func Writer(ctx context.Context) http.ResponseWriter {
data := ctx.Value(writerKey)
w, ok := data.(http.ResponseWriter)
if !ok {
return nil
}
return w
}