-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsuite.go
186 lines (160 loc) · 3.8 KB
/
msuite.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package msuite
import (
"encoding/base64"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/plexsysio/go-msuite/core"
"github.com/plexsysio/go-msuite/modules/config"
jsonConf "github.com/plexsysio/go-msuite/modules/config/json"
"github.com/plexsysio/go-msuite/modules/node"
)
type BuildCfg struct {
startupCfg config.Config
}
type Option func(c *BuildCfg)
func WithGRPC(nw string, id interface{}) Option {
return func(c *BuildCfg) {
c.startupCfg.Set("UseGRPC", true)
switch nw {
case "tcp":
c.startupCfg.Set("UseTCP", true)
c.startupCfg.Set("TCPPort", id.(int))
case "p2p":
c.startupCfg.Set("UseP2PGRPC", true)
case "unix":
c.startupCfg.Set("UseUDS", true)
c.startupCfg.Set("UDSocket", id.(string))
}
}
}
func WithAuth(secret string) Option {
return func(c *BuildCfg) {
c.startupCfg.Set("UseAuth", true)
c.startupCfg.Set("JWTSecret", secret)
}
}
func WithTracing(name, host string) Option {
return func(c *BuildCfg) {
c.startupCfg.Set("UseTracing", true)
c.startupCfg.Set("TracingName", name)
c.startupCfg.Set("TracingHost", host)
}
}
func WithHTTP(port int) Option {
return func(c *BuildCfg) {
c.startupCfg.Set("UseHTTP", true)
c.startupCfg.Set("HTTPPort", port)
}
}
func WithLocker(lkr string, cfg map[string]string) Option {
return func(c *BuildCfg) {
c.startupCfg.Set("UseLocker", true)
c.startupCfg.Set("Locker", lkr)
for k, v := range cfg {
c.startupCfg.Set(k, v)
}
}
}
func WithP2PPrivateKey(key crypto.PrivKey) Option {
return func(c *BuildCfg) {
skbytes, err := crypto.MarshalPrivateKey(key)
if err != nil {
return
}
ident := map[string]interface{}{}
ident["PrivKey"] = base64.StdEncoding.EncodeToString(skbytes)
id, err := peer.IDFromPublicKey(key.GetPublic())
if err != nil {
return
}
ident["ID"] = id.Pretty()
c.startupCfg.Set("Identity", ident)
}
}
func WithP2P(port int) Option {
return func(c *BuildCfg) {
c.startupCfg.Set("UseP2P", true)
c.startupCfg.Set("SwarmPort", port)
}
}
func WithRepositoryRoot(path string) Option {
return func(c *BuildCfg) {
c.startupCfg.Set("RootPath", path)
}
}
func WithServices(services ...string) Option {
return func(c *BuildCfg) {
c.startupCfg.Set("Services", services)
}
}
func WithServiceACL(acl map[string]string) Option {
return func(c *BuildCfg) {
existingAcls := map[string]string{}
_ = c.startupCfg.Get("ACL", &existingAcls)
for k, v := range acl {
existingAcls[k] = v
}
c.startupCfg.Set("ACL", existingAcls)
}
}
func WithTaskManager(min, max int) Option {
return func(c *BuildCfg) {
if max < 20 {
max += 20
}
c.startupCfg.Set("TMWorkers", map[string]int{
"Min": min,
"Max": max,
})
}
}
func WithPrometheus(useLatency bool) Option {
return func(c *BuildCfg) {
c.startupCfg.Set("UsePrometheus", true)
if useLatency {
c.startupCfg.Set("UsePrometheusLatency", true)
}
}
}
func WithStaticDiscovery(svcAddrs map[string]string) Option {
return func(c *BuildCfg) {
c.startupCfg.Set("UseStaticDiscovery", true)
c.startupCfg.Set("StaticAddresses", svcAddrs)
}
}
func WithDebug() Option {
return func(c *BuildCfg) {
c.startupCfg.Set("UseDebug", true)
}
}
func WithFiles() Option {
return func(c *BuildCfg) {
c.startupCfg.Set("UseFiles", true)
}
}
func WithBootstrapNodes(addrs ...string) Option {
return func(c *BuildCfg) {
if len(addrs) > 0 {
c.startupCfg.Set("BootstrapAddresses", addrs)
}
}
}
func defaultOpts(c *BuildCfg) {
if !c.startupCfg.Exists("Services") {
c.startupCfg.Set("Services", []string{"msuite"})
}
}
func New(opts ...Option) (core.Service, error) {
bCfg := &BuildCfg{
startupCfg: jsonConf.DefaultConfig(),
}
for _, opt := range opts {
opt(bCfg)
}
defaultOpts(bCfg)
svc, err := node.New(bCfg.startupCfg)
if err != nil {
return nil, err
}
return svc, nil
}