-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
136 lines (117 loc) · 2.41 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package service
import (
"context"
"crypto"
"crypto/tls"
"crypto/x509"
"github.com/omecodes/common/errors"
"github.com/omecodes/libome"
"google.golang.org/grpc"
)
type ctxBox struct{}
type ctxServiceCredentials struct{}
func serviceBox(ctx context.Context) *Box {
o := ctx.Value(ctxBox{})
if o == nil {
return nil
}
return o.(*Box)
}
func GetCACertificate(ctx context.Context) *x509.Certificate {
box := serviceBox(ctx)
if box == nil {
return nil
}
return box.caCert
}
func GetCertificate(ctx context.Context) *x509.Certificate {
box := serviceBox(ctx)
if box == nil {
return nil
}
return box.cert
}
func GetPrivateKey(ctx context.Context) crypto.PrivateKey {
box := serviceBox(ctx)
if box == nil {
return nil
}
return box.key
}
func GetRegistry(ctx context.Context) ome.Registry {
box := serviceBox(ctx)
if box == nil {
return nil
}
return box.registry
}
func CACredentials(ctx context.Context) *ome.ProxyCredentials {
box := serviceBox(ctx)
if box == nil {
return nil
}
return &ome.ProxyCredentials{
Key: box.Options.caAPIKey,
Secret: box.Options.caAPISecret,
}
}
// Secret returns the application shared secret
func GetSecret(ctx context.Context) string {
box := serviceBox(ctx)
if box == nil {
return ""
}
return box.caAPISecret
}
func GetName(ctx context.Context) string {
box := serviceBox(ctx)
if box == nil {
return ""
}
return box.Options.name
}
func GetDir(ctx context.Context) string {
box := serviceBox(ctx)
if box == nil {
return ""
}
return box.Options.workingDir
}
func GetID(ctx context.Context) string {
box := serviceBox(ctx)
if box == nil {
return ""
}
return box.Options.name
}
func GetGatewayAddress(ctx context.Context, name string) string {
box := serviceBox(ctx)
if box == nil {
return ""
}
gt, ok := box.httpNodes[name]
if !ok {
return ""
}
return gt.URL()
}
func GetClientTLSConfig(ctx context.Context) (*tls.Config, error) {
box := serviceBox(ctx)
if box == nil {
return nil, errors.NotSupported
}
return box.ClientMutualTLS()
}
func Dial(ctx context.Context, st uint32) (*grpc.ClientConn, error) {
box := serviceBox(ctx)
if box == nil {
return nil, errors.New("no service box associated to context")
}
return box.dialToService(st)
}
func ContextWithBox(ctx context.Context, b *Box) context.Context {
return context.WithValue(ctx, ctxBox{}, b)
}
func BoxFromContext(ctx context.Context) *Box {
return serviceBox(ctx)
}