-
Notifications
You must be signed in to change notification settings - Fork 9
/
context.go
42 lines (36 loc) · 1.18 KB
/
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
package httpjson
import (
"context"
"net/http"
)
// key is an unexported type for keys defined in this package.
// This prevents collisions with keys defined in other packages.
type key int
// Keys for HTTP objects in Contexts.
// They are unexported; clients use Request and ResponseWriter
// instead of using these keys directly.
const (
reqKey key = iota
respKey
)
// Request returns the HTTP request stored in ctx.
// If there is none, it panics.
// The context given to a handler function
// registered in this package is guaranteed to have
// a request.
func Request(ctx context.Context) *http.Request {
return ctx.Value(reqKey).(*http.Request)
}
// ResponseWriter returns the HTTP response writer stored in ctx.
// If there is none, it panics.
// The context given to a handler function
// registered in this package is guaranteed to have
// a response writer.
func ResponseWriter(ctx context.Context) http.ResponseWriter {
return ctx.Value(respKey).(http.ResponseWriter)
}
// WithRequest returns a context with an HTTP request stored in it.
// It is useful for testing.
func WithRequest(ctx context.Context, req *http.Request) context.Context {
return context.WithValue(ctx, reqKey, req)
}