-
Notifications
You must be signed in to change notification settings - Fork 9
/
config.go
353 lines (290 loc) · 8.19 KB
/
config.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package service
import (
"os"
"path/filepath"
api_gen "github.com/gnolang/gnonative/api/gen/go"
"github.com/pkg/errors"
"go.uber.org/zap"
)
const DEFAULT_TCP_ADDR = ":26658"
const DEFAULT_SOCKET_SUBDIR = "s"
const DEFAULT_SOCKET_FILE = "gno"
// Config describes a set of settings for a GnoNativeService
type Config struct {
Logger *zap.Logger
Remote string
ChainID string
RootDir string
TmpDir string
TcpAddr string
UdsPath string
UseTcpListener bool
DisableUdsListener bool
}
type GnoNativeOption func(cfg *Config) error
func (cfg *Config) applyOptions(opts ...GnoNativeOption) error {
withDefaultOpts := make([]GnoNativeOption, len(opts))
copy(withDefaultOpts, opts)
withDefaultOpts = append(withDefaultOpts, WithFallbackDefaults)
for _, opt := range withDefaultOpts {
if err := opt(cfg); err != nil {
return err
}
}
return nil
}
func (cfg *Config) checkDirs() error {
// check if rootDir exists
{
_, err := os.Stat(cfg.RootDir)
if os.IsNotExist(err) {
return errors.Wrap(err, "rootDir folder doesn't exist")
}
}
// check if tmpDir exists
{
_, err := os.Stat(cfg.TmpDir)
if os.IsNotExist(err) {
return errors.Wrap(err, "tmpDir folder doesn't exist")
}
}
return nil
}
// FallBackOption is a structure that permits to fallback to a default option if the option is not set.
type FallBackOption struct {
fallback func(cfg *Config) bool
opt GnoNativeOption
}
// --- Logger options ---
// WithLogger set the given logger.
var WithLogger = func(l *zap.Logger) GnoNativeOption {
return func(cfg *Config) error {
cfg.Logger = l
return nil
}
}
// WithDefaultLogger init a noop logger.
var WithDefaultLogger GnoNativeOption = func(cfg *Config) error {
logger, err := zap.NewDevelopment()
if err != nil {
return err
}
cfg.Logger = logger
return nil
}
var fallbackLogger = FallBackOption{
fallback: func(cfg *Config) bool { return cfg.Logger == nil },
opt: WithDefaultLogger,
}
// WithFallbackLogger sets the logger if no logger is set.
var WithFallbackLogger GnoNativeOption = func(cfg *Config) error {
if fallbackLogger.fallback(cfg) {
return fallbackLogger.opt(cfg)
}
return nil
}
// --- Remote options ---
// WithRemote sets the given remote node address.
var WithRemote = func(remote string) GnoNativeOption {
return func(cfg *Config) error {
cfg.Remote = remote
return nil
}
}
// WithDefaultRemote inits a default remote node address.
var WithDefaultRemote GnoNativeOption = func(cfg *Config) error {
cfg.Remote = "127.0.0.1:26657"
return nil
}
var fallbackRemote = FallBackOption{
fallback: func(cfg *Config) bool { return cfg.Remote == "" },
opt: WithDefaultRemote,
}
// WithFallbackRemote sets the remote node address if no address is set.
var WithFallbacRemote GnoNativeOption = func(cfg *Config) error {
if fallbackRemote.fallback(cfg) {
return fallbackRemote.opt(cfg)
}
return nil
}
// --- ChainID options ---
// WithChainID sets the given chain ID.
var WithChainID = func(chainID string) GnoNativeOption {
return func(cfg *Config) error {
cfg.ChainID = chainID
return nil
}
}
// WithDefaultChainID sets a default chain ID.
var WithDefaultChainID GnoNativeOption = func(cfg *Config) error {
cfg.ChainID = "dev"
return nil
}
var fallbackChainID = FallBackOption{
fallback: func(cfg *Config) bool { return cfg.ChainID == "" },
opt: WithDefaultChainID,
}
// WithFallbackChainID sets the chain ID if no chain ID is set.
var WithFallbacChainID GnoNativeOption = func(cfg *Config) error {
if fallbackChainID.fallback(cfg) {
return fallbackChainID.opt(cfg)
}
return nil
}
// --- RootDir options ---
// WithRootDir sets the given root directory path.
var WithRootDir = func(rootDir string) GnoNativeOption {
return func(cfg *Config) error {
cfg.RootDir = rootDir
return nil
}
}
// WithDefaultRootDir sets a default root directory in a temporary folder.
var WithDefaultRootDir GnoNativeOption = func(cfg *Config) error {
rootDir, err := os.MkdirTemp("", "gnonative")
if err != nil {
return err
}
cfg.RootDir = rootDir
return nil
}
var fallbackRootDir = FallBackOption{
fallback: func(cfg *Config) bool { return cfg.RootDir == "" },
opt: WithDefaultRootDir,
}
// WithFallbackRootDir sets the default root directory if no directory is set.
var WithFallbackRootDir GnoNativeOption = func(cfg *Config) error {
if fallbackRootDir.fallback(cfg) {
return fallbackRootDir.opt(cfg)
}
return nil
}
// --- tmpDir options ---
// WithTmpDir sets the given temporary path.
var WithTmpDir = func(path string) GnoNativeOption {
return func(cfg *Config) error {
cfg.TmpDir = path
return nil
}
}
// WithDefaultTmpDir sets a default temporary path.
var WithDefaultTmpDir GnoNativeOption = func(cfg *Config) error {
// dependency
if err := WithFallbackRootDir(cfg); err != nil {
return err
}
cfg.TmpDir = cfg.RootDir
return nil
}
var fallbackTmpDir = FallBackOption{
fallback: func(cfg *Config) bool { return cfg.TmpDir == "" },
opt: WithDefaultTmpDir,
}
// WithFallbackTmpDir sets the default temporary path if no path is set.
var WithFallbackTmpDir GnoNativeOption = func(cfg *Config) error {
if fallbackTmpDir.fallback(cfg) {
return fallbackTmpDir.opt(cfg)
}
return nil
}
// --- tcpAddr options ---
// WithTcpAddr sets the given TCP address to serve the gRPC server.
// If no TCP address is defined, a default will be used.
// If the TCP port is set to 0, a random port number will be chosen.
var WithTcpAddr = func(addr string) GnoNativeOption {
return func(cfg *Config) error {
cfg.TcpAddr = addr
return nil
}
}
// WithDefaultTcpAddr sets a default TCP addr to listen to.
var WithDefaultTcpAddr GnoNativeOption = func(cfg *Config) error {
cfg.TcpAddr = DEFAULT_TCP_ADDR
return nil
}
var fallbackTcpAddr = FallBackOption{
fallback: func(cfg *Config) bool { return cfg.TcpAddr == "" },
opt: WithDefaultTcpAddr,
}
// WithDefaultTcpAddr sets a default TCP addr to listen to if no address is set.
var WithFallbackTcpAddr GnoNativeOption = func(cfg *Config) error {
if fallbackTcpAddr.fallback(cfg) {
return fallbackTcpAddr.opt(cfg)
}
return nil
}
// --- udsPath options ---
// WithUdsPath sets the given Unix Domain Socket path to serve the gRPC server.
// If no UDS socket is defined, a default will be used.
var WithUdsPath = func(path string) GnoNativeOption {
return func(cfg *Config) error {
absPath, err := filepath.Abs(path)
if err != nil {
return err
}
cfg.UdsPath = absPath
return nil
}
}
// WithDefaultUdsPath sets a default UDS path to listen to.
var WithDefaultUdsPath GnoNativeOption = func(cfg *Config) error {
// dependency
if err := WithFallbackTmpDir(cfg); err != nil {
return err
}
// create a socket subdirectory
sockDir := filepath.Join(cfg.TmpDir, DEFAULT_SOCKET_SUBDIR)
if err := os.MkdirAll(sockDir, 0700); err != nil {
return api_gen.ErrCode_ErrInitService.Wrap(err)
}
cfg.UdsPath = filepath.Join(sockDir, DEFAULT_SOCKET_FILE)
return nil
}
var fallbackUdsPath = FallBackOption{
fallback: func(cfg *Config) bool { return cfg.UdsPath == "" },
opt: WithDefaultUdsPath,
}
// WithDefaultUdsPath sets a default UDS path to listen to if no path is set.
var WithFallbackUdsPath GnoNativeOption = func(cfg *Config) error {
if fallbackUdsPath.fallback(cfg) {
return fallbackUdsPath.opt(cfg)
}
return nil
}
// --- listener options ---
// WithUseTcpListener sets the gRPC server to serve on a TCP listener.
var WithUseTcpListener = func() GnoNativeOption {
return func(cfg *Config) error {
cfg.UseTcpListener = true
return nil
}
}
// WithDisableUdsListener sets the gRPC server to serve on a TCP listener.
var WithDisableUdsListener = func() GnoNativeOption {
return func(cfg *Config) error {
cfg.DisableUdsListener = true
return nil
}
}
// --- Fallback options ---
var defaults = []FallBackOption{
fallbackLogger,
fallbackRemote,
fallbackChainID,
fallbackRootDir,
fallbackTmpDir,
fallbackTcpAddr,
fallbackUdsPath,
}
// WithFallbackDefaults sets the default options if no option is set.
var WithFallbackDefaults GnoNativeOption = func(cfg *Config) error {
for _, def := range defaults {
if !def.fallback(cfg) {
continue
}
if err := def.opt(cfg); err != nil {
return err
}
}
return nil
}