-
Notifications
You must be signed in to change notification settings - Fork 34
/
context.go
102 lines (83 loc) · 2.48 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
package httptransport
import (
"context"
"net/http"
"os"
"github.com/machinefi/w3bstream/pkg/depends/base/consts"
"github.com/machinefi/w3bstream/pkg/depends/kit/kit"
"github.com/machinefi/w3bstream/pkg/depends/x/contextx"
)
// TODO move to conf
type ServiceMeta struct {
Name string
Version string
}
func (s *ServiceMeta) SetDefault() {
if s.Name == "" {
s.Name = os.Getenv(consts.EnvProjectName)
}
if s.Version == "" {
s.Version = os.Getenv(consts.EnvProjectVersion)
}
}
func (s ServiceMeta) String() string {
if s.Version == "" {
return s.Name
}
return s.Name + "@" + s.Version
}
type (
keyHttpRequest struct{} // keyHttpRequest pass original *http.Request
keyServiceMeta struct{} // keyServiceMeta pass service meta
keyRouteMetaID struct{}
ckOperatorFactory struct{}
ckQueryInBoyForGet struct{}
ckForRequestOut struct{}
)
func ContextWithHttpRequest(ctx context.Context, req *http.Request) context.Context {
return contextx.WithValue(ctx, keyHttpRequest{}, req)
}
func HttpRequestFromContext(ctx context.Context) *http.Request {
r, _ := ctx.Value(keyHttpRequest{}).(*http.Request)
return r
}
func ContextWithServiceMeta(ctx context.Context, meta ServiceMeta) context.Context {
return contextx.WithValue(ctx, keyServiceMeta{}, meta)
}
func ServiceMetaFromContext(ctx context.Context) ServiceMeta {
r, _ := ctx.Value(keyServiceMeta{}).(ServiceMeta)
return r
}
func ContextWithRouteMetaID(ctx context.Context, id string) context.Context {
return contextx.WithValue(ctx, keyRouteMetaID{}, id)
}
func OperationIDFromContext(ctx context.Context) string {
r, _ := ctx.Value(keyRouteMetaID{}).(string)
return r
}
func ContextWithOperatorFactory(ctx context.Context, factory *kit.OperatorFactory) context.Context {
return contextx.WithValue(ctx, ckOperatorFactory{}, factory)
}
func OperatorFactoryFromContext(ctx context.Context) *kit.OperatorFactory {
r, _ := ctx.Value(ckOperatorFactory{}).(*kit.OperatorFactory)
return r
}
func EnableQueryInBodyForGet(ctx context.Context) context.Context {
return contextx.WithValue(ctx, ckQueryInBoyForGet{}, true)
}
func ShouldQueryInBodyForGet(ctx context.Context) bool {
v, ok := ctx.Value(ckQueryInBoyForGet{}).(bool)
return ok && v
}
func AsRequestOut(ctx context.Context) context.Context {
return contextx.WithValue(ctx, ckForRequestOut{}, true)
}
func IsRequestOut(ctx context.Context) bool {
if ctx == nil {
return false
}
if t, ok := ctx.Value(ckForRequestOut{}).(bool); ok {
return t
}
return false
}