/
ctx.go
72 lines (58 loc) · 2.08 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
package handlers
import (
"context"
"net/http"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/rarimo/proof-verification-relayer/internal/config"
"github.com/rarimo/proof-verification-relayer/internal/contracts"
"gitlab.com/distributed_lab/logan/v3"
)
type ctxKey int
const (
logCtxKey ctxKey = iota
networkConfigCtxKey
ethClientCtxKey
voteVerifierRegisterMethodCtxKey
votingRegistryCtxKey
)
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 CtxNetworkConfig(cfg *config.NetworkConfig) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, networkConfigCtxKey, cfg)
}
}
func NetworkConfig(r *http.Request) *config.NetworkConfig {
return r.Context().Value(networkConfigCtxKey).(*config.NetworkConfig)
}
func CtxEthClient(client *ethclient.Client) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, ethClientCtxKey, client)
}
}
func EthClient(r *http.Request) *ethclient.Client {
return r.Context().Value(ethClientCtxKey).(*ethclient.Client)
}
func CtxVoteVerifierRegisterMethod(method *abi.Method) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, voteVerifierRegisterMethodCtxKey, method)
}
}
func VoteVerifierRegisterMethod(r *http.Request) *abi.Method {
return r.Context().Value(voteVerifierRegisterMethodCtxKey).(*abi.Method)
}
func CtxVotingRegistry(registry *contracts.VotingRegistry) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, votingRegistryCtxKey, registry)
}
}
func VotingRegistry(r *http.Request) *contracts.VotingRegistry {
return r.Context().Value(votingRegistryCtxKey).(*contracts.VotingRegistry)
}