/
ctx.go
123 lines (95 loc) · 3.19 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
117
118
119
120
121
122
123
package helpers
import (
"context"
"database/sql"
"net/http"
"github.com/acs-dl/orchestrator-svc/internal/config"
"gitlab.com/distributed_lab/logan/v3"
"github.com/ThreeDotsLabs/watermill-amqp/v2/pkg/amqp"
"github.com/acs-dl/orchestrator-svc/internal/data"
"github.com/acs-dl/orchestrator-svc/internal/sender"
)
type ctxKey int
const (
logCtxKey ctxKey = iota
modulesQCtxKey
requestsQCtxKey
requestTransactionsQCtxKey
senderCtxKey
rawDBCtxKey
publisherCtxKey
subscriberCtxKey
amqpCfgCtxKey
)
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 CtxModulesQ(entry data.ModuleQ) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, modulesQCtxKey, entry)
}
}
func ModulesQ(r *http.Request) data.ModuleQ {
return r.Context().Value(modulesQCtxKey).(data.ModuleQ)
}
func CtxRequestsQ(entry data.RequestQ) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, requestsQCtxKey, entry)
}
}
func RequestsQ(r *http.Request) data.RequestQ {
return r.Context().Value(requestsQCtxKey).(data.RequestQ)
}
func CtxRequestTransactionsQ(entry data.RequestTransactions) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, requestTransactionsQCtxKey, entry)
}
}
func RequestTransactionsQ(r *http.Request) data.RequestTransactions {
return r.Context().Value(requestTransactionsQCtxKey).(data.RequestTransactions)
}
func CtxSender(entry *sender.Sender) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, senderCtxKey, entry)
}
}
func Sender(r *http.Request) *sender.Sender {
return r.Context().Value(senderCtxKey).(*sender.Sender)
}
func CtxRawDB(entry *sql.DB) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, rawDBCtxKey, entry)
}
}
func RawDB(r *http.Request) *sql.DB {
return r.Context().Value(rawDBCtxKey).(*sql.DB)
}
func CtxPublisher(entry *amqp.Publisher) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, publisherCtxKey, entry)
}
}
func Publisher(r *http.Request) *amqp.Publisher {
return r.Context().Value(publisherCtxKey).(*amqp.Publisher)
}
func CtxSubscriber(entry *amqp.Subscriber) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, subscriberCtxKey, entry)
}
}
func Subscriber(r *http.Request) *amqp.Subscriber {
return r.Context().Value(subscriberCtxKey).(*amqp.Subscriber)
}
func CtxAmqpCfg(entry *config.AmqpCfg) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, amqpCfgCtxKey, entry)
}
}
func AmqpCfg(r *http.Request) *config.AmqpCfg {
return r.Context().Value(amqpCfgCtxKey).(*config.AmqpCfg)
}