forked from go-courier/httptransport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_ctx.go
89 lines (67 loc) · 2.15 KB
/
http_ctx.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package httptransport
import (
"context"
"net/http"
"os"
"github.com/go-courier/courier"
contextx "github.com/go-courier/x/context"
)
type ServiceMeta struct {
Name string
Version string
}
func (s *ServiceMeta) SetDefaults() {
if s.Name == "" {
s.Name = os.Getenv("PROJECT_NAME")
}
if s.Version == "" {
s.Version = os.Getenv("PROJECT_VERSION")
}
}
func (s ServiceMeta) String() string {
if s.Version == "" {
return s.Name
}
return s.Name + "@" + s.Version
}
type contextKeyHttpRequestKey struct{}
func ContextWithHttpRequest(ctx context.Context, req *http.Request) context.Context {
return contextx.WithValue(ctx, contextKeyHttpRequestKey{}, req)
}
func HttpRequestFromContext(ctx context.Context) *http.Request {
p, _ := ctx.Value(contextKeyHttpRequestKey{}).(*http.Request)
return p
}
type contextKeyServiceMetaKey struct{}
func ContextWithServiceMeta(ctx context.Context, meta ServiceMeta) context.Context {
return contextx.WithValue(ctx, contextKeyServiceMetaKey{}, meta)
}
func ServerMetaFromContext(ctx context.Context) ServiceMeta {
p, _ := ctx.Value(contextKeyServiceMetaKey{}).(ServiceMeta)
return p
}
type contextKeyOperationID struct{}
func ContextWithOperationID(ctx context.Context, operationID string) context.Context {
return contextx.WithValue(ctx, contextKeyOperationID{}, operationID)
}
func OperationIDFromContext(ctx context.Context) string {
return ctx.Value(contextKeyOperationID{}).(string)
}
type contextKeyOperatorFactory struct{}
func ContextWithOperatorFactory(ctx context.Context, om *courier.OperatorFactory) context.Context {
return contextx.WithValue(ctx, contextKeyOperatorFactory{}, om)
}
func OperatorFactoryFromContext(ctx context.Context) *courier.OperatorFactory {
v, _ := ctx.Value(contextKeyOperatorFactory{}).(*courier.OperatorFactory)
return v
}
type contextKeyQueryInBodyForHttpGet struct{}
func EnableQueryInBodyForHttpGet(ctx context.Context) context.Context {
return contextx.WithValue(ctx, contextKeyQueryInBodyForHttpGet{}, true)
}
func ShouldQueryInBodyForHttpGet(ctx context.Context) bool {
if v, ok := ctx.Value(contextKeyQueryInBodyForHttpGet{}).(bool); ok {
return v
}
return false
}