summaryrefslogtreecommitdiff
path: root/ctxlib/context.go
blob: 79851ced7a1df6ab6374d7f23e44870a15d5237b (plain) (blame)
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
package ctxlib

import (
	"context"
	"net/http"
)

type ctxKey int

const (
	Method ctxKey = iota
	Headers
	Request
	RequestID
	Response
)

func ResponseFromContext(ctx context.Context) http.ResponseWriter {
	return ctx.Value(Response).(http.ResponseWriter)
}

func RequestFromContext(ctx context.Context) *http.Request {
	c := ctx.Value(Request)
	if c == nil {
		return &http.Request{}
	}
	return c.(*http.Request)
}

func HeadersFromContext(ctx context.Context) http.Header {
	c := ctx.Value(Headers)
	if c == nil {
		return http.Header{}
	}
	return c.(http.Header)
}

func MethodFromContext(ctx context.Context) string {
	c := ctx.Value(Method)
	if c == nil {
		return http.MethodGet
	}
	return c.(string)
}