This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
client_options.go
524 lines (458 loc) · 14.6 KB
/
client_options.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
package bux
import (
"database/sql"
"strings"
"time"
"github.com/BuxOrg/bux/cachestore"
"github.com/BuxOrg/bux/chainstate"
"github.com/BuxOrg/bux/datastore"
"github.com/BuxOrg/bux/taskmanager"
"github.com/OrlovEvgeny/go-mcache"
"github.com/dgraph-io/ristretto"
"github.com/go-redis/redis/v8"
"github.com/mrz1836/go-cache"
"github.com/newrelic/go-agent/v3/newrelic"
"github.com/tonicpow/go-paymail"
"github.com/tonicpow/go-paymail/server"
"github.com/vmihailenco/taskq/v3"
"go.mongodb.org/mongo-driver/mongo"
"gorm.io/gorm/logger"
)
// ClientOps allow functional options to be supplied
// that overwrite default client options.
type ClientOps func(c *clientOptions)
// defaultClientOptions will return an clientOptions struct with the default settings
//
// Useful for starting with the default and then modifying as needed
func defaultClientOptions() *clientOptions {
// Set the default options
return &clientOptions{
// Incoming Transaction Checker (lookup external tx via miner for validity)
itc: true,
// By default check input utxos (unless disabled by the user)
iuc: true,
// Blank chainstate config
chainstate: &chainstateOptions{
ClientInterface: nil,
options: []chainstate.ClientOps{},
},
// Blank cache config
cacheStore: &cacheStoreOptions{
ClientInterface: nil,
options: []cachestore.ClientOps{},
},
// Blank Datastore config
dataStore: &dataStoreOptions{
ClientInterface: nil,
options: []datastore.ClientOps{},
},
// Blank model options (use the Base models)
models: &modelOptions{
modelNames: modelNames(BaseModels...),
models: BaseModels,
migrateModelNames: nil,
migrateModels: nil,
},
// Blank NewRelic config
newRelic: &newRelicOptions{},
// Blank Paymail config
paymail: &paymailOptions{
client: nil,
serverConfig: &paymailServerOptions{
Configuration: nil,
},
},
// Blank TaskManager config
taskManager: &taskManagerOptions{
ClientInterface: nil,
cronTasks: map[string]time.Duration{
ModelDraftTransaction.String() + "_clean_up": 60 * time.Second,
ModelIncomingTransaction.String() + "_process": 30 * time.Second,
ModelSyncTransaction.String() + "_broadcast": 30 * time.Second,
ModelSyncTransaction.String() + "_sync": 30 * time.Second,
},
},
// Default user agent
userAgent: defaultUserAgent,
}
}
// modelNames will take a list of models and return the list of names
func modelNames(models ...interface{}) (names []string) {
for _, modelInterface := range models {
names = append(names, modelInterface.(ModelInterface).Name())
}
return
}
// modelExists will return true if the model is found
func (o *clientOptions) modelExists(modelName, list string) bool {
m := o.models.modelNames
if list == migrateList {
m = o.models.migrateModelNames
}
for _, name := range m {
if strings.EqualFold(name, modelName) {
return true
}
}
return false
}
// addModel will add the model if it does not exist already (load once)
func (o *clientOptions) addModel(model interface{}, list string) {
name := model.(ModelInterface).Name()
if !o.modelExists(name, list) {
if list == migrateList {
o.models.migrateModelNames = append(o.models.migrateModelNames, name)
o.models.migrateModels = append(o.models.migrateModels, model)
return
}
o.models.modelNames = append(o.models.modelNames, name)
o.models.models = append(o.models.models, model)
}
}
// addModels will add the models if they do not exist already (load once)
func (o *clientOptions) addModels(list string, models ...interface{}) {
for _, modelInterface := range models {
o.addModel(modelInterface, list)
}
}
// DefaultModelOptions will set any default model options (from Client options->model)
func (c *Client) DefaultModelOptions(opts ...ModelOps) []ModelOps {
// Set the Client from the bux.Client onto the model
opts = append(opts, WithClient(c))
// Return the new options
return opts
}
// -----------------------------------------------------------------
// GENERAL
// -----------------------------------------------------------------
// WithUserAgent will overwrite the default useragent
func WithUserAgent(userAgent string) ClientOps {
return func(c *clientOptions) {
if len(userAgent) > 0 {
c.userAgent = userAgent
}
}
}
// WithNewRelic will set the NewRelic application client
func WithNewRelic(app *newrelic.Application) ClientOps {
return func(c *clientOptions) {
// Disregard if the app is nil
if app == nil {
return
}
// Set the app
c.newRelic.app = app
// Enable New relic on other services
if c.chainstate != nil {
c.chainstate.options = append(c.chainstate.options, chainstate.WithNewRelic())
}
if c.cacheStore != nil {
c.cacheStore.options = append(c.cacheStore.options, cachestore.WithNewRelic())
}
if c.dataStore != nil {
c.dataStore.options = append(c.dataStore.options, datastore.WithNewRelic())
}
if c.taskManager != nil {
c.taskManager.options = append(c.taskManager.options, taskmanager.WithNewRelic())
}
// Enable the service
c.newRelic.enabled = true
}
}
// WithDebugging will set debugging in any applicable configuration
func WithDebugging() ClientOps {
return func(c *clientOptions) {
c.debug = true
// Enable debugging on other services
if c.chainstate != nil {
c.chainstate.options = append(c.chainstate.options, chainstate.WithDebugging())
}
if c.cacheStore != nil {
c.cacheStore.options = append(c.cacheStore.options, cachestore.WithDebugging())
}
if c.dataStore != nil {
c.dataStore.options = append(c.dataStore.options, datastore.WithDebugging())
}
if c.taskManager != nil {
c.taskManager.options = append(c.taskManager.options, taskmanager.WithDebugging())
}
}
}
// WithModels will add additional models (will NOT migrate using datastore)
//
// Pointers of structs (IE: &models.Xpub{})
func WithModels(models ...interface{}) ClientOps {
return func(c *clientOptions) {
if len(models) > 0 {
c.addModels(modelList, models...)
}
}
}
// WithITCDisabled will disable (ITC) incoming transaction checking
func WithITCDisabled() ClientOps {
return func(c *clientOptions) {
c.itc = false
}
}
// WithIUCDisabled will disable checking the input utxos
func WithIUCDisabled() ClientOps {
return func(c *clientOptions) {
c.iuc = false
}
}
// WithLogger will set the custom logger interface
func WithLogger(customLogger logger.Interface) ClientOps {
return func(c *clientOptions) {
if customLogger != nil {
c.logger = customLogger
// Enable debugging on other services
if c.chainstate != nil {
c.chainstate.options = append(c.chainstate.options, chainstate.WithLogger(c.logger))
}
if c.taskManager != nil {
c.taskManager.options = append(c.taskManager.options, taskmanager.WithLogger(c.logger))
}
if c.dataStore != nil {
c.dataStore.options = append(c.dataStore.options, datastore.WithLogger(c.logger))
}
}
}
}
// -----------------------------------------------------------------
// CACHESTORE
// -----------------------------------------------------------------
// WithCustomCachestore will set the cachestore
func WithCustomCachestore(cacheStore cachestore.ClientInterface) ClientOps {
return func(c *clientOptions) {
if cacheStore != nil {
c.cacheStore.ClientInterface = cacheStore
}
}
}
// WithMcache will set the cache client for both Read & Write clients
func WithMcache() ClientOps {
return func(c *clientOptions) {
c.cacheStore.options = append(c.cacheStore.options, cachestore.WithMcache())
}
}
// WithMcacheConnection will set the cache client to an active mcache driver connection
func WithMcacheConnection(driver *mcache.CacheDriver) ClientOps {
return func(c *clientOptions) {
if driver != nil {
c.cacheStore.options = append(
c.cacheStore.options,
cachestore.WithMcacheConnection(driver),
)
}
}
}
// WithRistretto will set the cache client for both Read & Write clients
func WithRistretto(config *ristretto.Config) ClientOps {
return func(c *clientOptions) {
if config != nil {
c.cacheStore.options = append(c.cacheStore.options, cachestore.WithRistretto(config))
}
}
}
// WithRistrettoConnection will set the cache client to an active Ristretto connection
func WithRistrettoConnection(client *ristretto.Cache) ClientOps {
return func(c *clientOptions) {
if client != nil {
c.cacheStore.options = append(
c.cacheStore.options,
cachestore.WithRistrettoConnection(client),
)
}
}
}
// WithRedis will set the redis cache client for both Read & Write clients
//
// This will load new redis connections using the given parameters
func WithRedis(config *cachestore.RedisConfig) ClientOps {
return func(c *clientOptions) {
if config != nil {
c.cacheStore.options = append(c.cacheStore.options, cachestore.WithRedis(config))
}
}
}
// WithRedisConnection will set the cache client to an active redis connection
func WithRedisConnection(activeClient *cache.Client) ClientOps {
return func(c *clientOptions) {
if activeClient != nil {
c.cacheStore.options = append(
c.cacheStore.options,
cachestore.WithRedisConnection(activeClient),
)
}
}
}
// -----------------------------------------------------------------
// DATASTORE
// -----------------------------------------------------------------
// WithCustomDatastore will set the datastore
func WithCustomDatastore(dataStore datastore.ClientInterface) ClientOps {
return func(c *clientOptions) {
if dataStore != nil {
c.dataStore.ClientInterface = dataStore
}
}
}
// WithAutoMigrate will enable auto migrate database mode (given models)
//
// Pointers of structs (IE: &models.Xpub{})
func WithAutoMigrate(migrateModels ...interface{}) ClientOps {
return func(c *clientOptions) {
if len(migrateModels) > 0 {
c.addModels(modelList, migrateModels...)
c.addModels(migrateList, migrateModels...)
}
}
}
// WithSQLite will set the Datastore to use SQLite
func WithSQLite(config *datastore.SQLiteConfig) ClientOps {
return func(c *clientOptions) {
if config != nil {
c.dataStore.options = append(c.dataStore.options, datastore.WithSQLite(config))
}
}
}
// WithSQL will load a Datastore using either an SQL database config or existing connection
func WithSQL(engine datastore.Engine, config *datastore.SQLConfig) ClientOps {
return func(c *clientOptions) {
if config != nil && !engine.IsEmpty() {
c.dataStore.options = append(
c.dataStore.options,
datastore.WithSQL(engine, []*datastore.SQLConfig{config}),
)
}
}
}
// WithSQLConnection will set the Datastore to an existing connection for MySQL or PostgreSQL
func WithSQLConnection(engine datastore.Engine, sqlDB *sql.DB, tablePrefix string) ClientOps {
return func(c *clientOptions) {
if sqlDB != nil && !engine.IsEmpty() {
c.dataStore.options = append(
c.dataStore.options,
datastore.WithSQLConnection(engine, sqlDB, tablePrefix),
)
}
}
}
// WithMongoDB will set the Datastore to use MongoDB
func WithMongoDB(config *datastore.MongoDBConfig) ClientOps {
return func(c *clientOptions) {
if config != nil {
c.dataStore.options = append(c.dataStore.options, datastore.WithMongo(config))
}
}
}
// WithMongoConnection will set the Datastore to an existing connection for MongoDB
func WithMongoConnection(database *mongo.Database, tablePrefix string) ClientOps {
return func(c *clientOptions) {
if database != nil {
c.dataStore.options = append(
c.dataStore.options,
datastore.WithMongoConnection(database, tablePrefix),
)
}
}
}
// -----------------------------------------------------------------
// PAYMAIL
// -----------------------------------------------------------------
// WithPaymailClient will set a custom paymail client
func WithPaymailClient(client paymail.ClientInterface) ClientOps {
return func(c *clientOptions) {
if client != nil {
c.paymail.client = client
}
}
}
// WithPaymailServer will set the server configuration for Paymail
func WithPaymailServer(config *server.Configuration, defaultFromPaymail, defaultNote string) ClientOps {
return func(c *clientOptions) {
if config != nil {
c.paymail.serverConfig.Configuration = config
}
if len(defaultFromPaymail) > 0 {
c.paymail.serverConfig.DefaultFromPaymail = defaultFromPaymail
}
if len(defaultNote) > 0 {
c.paymail.serverConfig.DefaultNote = defaultNote
}
}
}
// -----------------------------------------------------------------
// TASK MANAGER
// -----------------------------------------------------------------
// WithCustomTaskManager will set the taskmanager
func WithCustomTaskManager(taskManager taskmanager.ClientInterface) ClientOps {
return func(c *clientOptions) {
if taskManager != nil {
c.taskManager.ClientInterface = taskManager
}
}
}
// WithTaskQ will set the task manager to use TaskQ & in-memory
func WithTaskQ(config *taskq.QueueOptions, factory taskmanager.Factory) ClientOps {
return func(c *clientOptions) {
if config != nil {
c.taskManager.options = append(
c.taskManager.options,
taskmanager.WithTaskQ(config, factory),
)
}
}
}
// WithTaskQUsingRedis will set the task manager to use TaskQ & Redis
func WithTaskQUsingRedis(config *taskq.QueueOptions, redisOptions *redis.Options) ClientOps {
return func(c *clientOptions) {
if config != nil {
// Create a new redis client
if config.Redis == nil {
// Remove prefix if found
redisOptions.Addr = strings.Replace(redisOptions.Addr, cachestore.RedisPrefix, "", -1)
config.Redis = redis.NewClient(redisOptions)
}
c.taskManager.options = append(
c.taskManager.options,
taskmanager.WithTaskQ(config, taskmanager.FactoryRedis),
)
}
}
}
// -----------------------------------------------------------------
// CHAIN-STATE
// -----------------------------------------------------------------
// WithCustomChainstate will set the chainstate
func WithCustomChainstate(chainState chainstate.ClientInterface) ClientOps {
return func(c *clientOptions) {
if chainState != nil {
c.chainstate.ClientInterface = chainState
}
}
}
// WithWhatsOnChainAPIKey will set the API key
func WithWhatsOnChainAPIKey(apiKey string) ClientOps {
return func(c *clientOptions) {
if len(apiKey) > 0 {
c.chainstate.options = append(c.chainstate.options, chainstate.WithWhatsOnChainAPIKey(apiKey))
}
}
}
// WithNowNodesAPIKey will set the API key
func WithNowNodesAPIKey(apiKey string) ClientOps {
return func(c *clientOptions) {
if len(apiKey) > 0 {
c.chainstate.options = append(c.chainstate.options, chainstate.WithNowNodesAPIKey(apiKey))
}
}
}
// WithMatterCloudAPIKey will set the API key
func WithMatterCloudAPIKey(apiKey string) ClientOps {
return func(c *clientOptions) {
if len(apiKey) > 0 {
c.chainstate.options = append(c.chainstate.options, chainstate.WithMatterCloudAPIKey(apiKey))
}
}
}