/
ctx.go
116 lines (94 loc) · 3.18 KB
/
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package handlers
import (
"context"
"net/http"
"github.com/rarimo/auth-svc/resources"
"github.com/rarimo/rarime-points-svc/internal/data"
"github.com/rarimo/rarime-points-svc/internal/data/evtypes"
"github.com/rarimo/saver-grpc-lib/broadcaster"
"gitlab.com/distributed_lab/logan/v3"
)
type ctxKey int
const (
logCtxKey ctxKey = iota
eventsQCtxKey
balancesQCtxKey
withdrawalsQCtxKey
referralsQCtxKey
eventTypesCtxKey
userClaimsCtxKey
broadcasterCtxKey
pointPriceCtxKey
)
func CtxLog(entry *logan.Entry) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, logCtxKey, entry)
}
}
func Log(r *http.Request) *logan.Entry {
return r.Context().Value(logCtxKey).(*logan.Entry)
}
func CtxEventsQ(q data.EventsQ) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, eventsQCtxKey, q)
}
}
func EventsQ(r *http.Request) data.EventsQ {
return r.Context().Value(eventsQCtxKey).(data.EventsQ).New()
}
func CtxBalancesQ(q data.BalancesQ) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, balancesQCtxKey, q)
}
}
func BalancesQ(r *http.Request) data.BalancesQ {
return r.Context().Value(balancesQCtxKey).(data.BalancesQ).New()
}
func CtxWithdrawalsQ(q data.WithdrawalsQ) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, withdrawalsQCtxKey, q)
}
}
func WithdrawalsQ(r *http.Request) data.WithdrawalsQ {
return r.Context().Value(withdrawalsQCtxKey).(data.WithdrawalsQ).New()
}
func CtxReferralsQ(q data.ReferralsQ) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, referralsQCtxKey, q)
}
}
func ReferralsQ(r *http.Request) data.ReferralsQ {
return r.Context().Value(referralsQCtxKey).(data.ReferralsQ).New()
}
func CtxEventTypes(types evtypes.Types) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, eventTypesCtxKey, types)
}
}
func EventTypes(r *http.Request) evtypes.Types {
return r.Context().Value(eventTypesCtxKey).(evtypes.Types)
}
func CtxUserClaims(claim []resources.Claim) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, userClaimsCtxKey, claim)
}
}
func UserClaims(r *http.Request) []resources.Claim {
return r.Context().Value(userClaimsCtxKey).([]resources.Claim)
}
func CtxBroadcaster(broadcaster broadcaster.Broadcaster) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, broadcasterCtxKey, broadcaster)
}
}
func Broadcaster(r *http.Request) broadcaster.Broadcaster {
return r.Context().Value(broadcasterCtxKey).(broadcaster.Broadcaster)
}
func CtxPointPrice(price int64) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, pointPriceCtxKey, price)
}
}
func PointPrice(r *http.Request) int64 {
return r.Context().Value(pointPriceCtxKey).(int64)
}