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.go
425 lines (358 loc) · 13.4 KB
/
client.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
package bux
import (
"context"
"time"
"github.com/BuxOrg/bux/chainstate"
"github.com/BuxOrg/bux/cluster"
"github.com/BuxOrg/bux/logging"
"github.com/BuxOrg/bux/metrics"
"github.com/BuxOrg/bux/notifications"
"github.com/BuxOrg/bux/taskmanager"
"github.com/bitcoin-sv/go-paymail"
"github.com/bitcoin-sv/go-paymail/server"
"github.com/mrz1836/go-cachestore"
"github.com/mrz1836/go-datastore"
"github.com/newrelic/go-agent/v3/newrelic"
"github.com/rs/zerolog"
)
type (
// Client is the bux client & options
Client struct {
options *clientOptions
}
// clientOptions holds all the configuration for the client
clientOptions struct {
cacheStore *cacheStoreOptions // Configuration options for Cachestore (ristretto, redis, etc.)
cluster *clusterOptions // Configuration options for the cluster coordinator
chainstate *chainstateOptions // Configuration options for Chainstate (broadcast, sync, etc.)
dataStore *dataStoreOptions // Configuration options for the DataStore (MySQL, etc.)
debug bool // If the client is in debug mode
encryptionKey string // Encryption key for encrypting sensitive information (IE: paymail xPub) (hex encoded key)
httpClient HTTPInterface // HTTP interface to use
iuc bool // (Input UTXO Check) True will check input utxos when saving transactions
logger *zerolog.Logger // Internal logging
metrics *metrics.Metrics // Metrics with a collector interface
models *modelOptions // Configuration options for the loaded models
newRelic *newRelicOptions // Configuration options for NewRelic
notifications *notificationsOptions // Configuration options for Notifications
paymail *paymailOptions // Paymail options & client
taskManager *taskManagerOptions // Configuration options for the TaskManager (TaskQ, etc.)
userAgent string // User agent for all outgoing requests
}
// chainstateOptions holds the chainstate configuration and client
chainstateOptions struct {
chainstate.ClientInterface // Client for Chainstate
options []chainstate.ClientOps // List of options
broadcasting bool // Default value for all transactions
broadcastInstant bool // Default value for all transactions
paymailP2P bool // Default value for all transactions
syncOnChain bool // Default value for all transactions
}
// cacheStoreOptions holds the cache configuration and client
cacheStoreOptions struct {
cachestore.ClientInterface // Client for Cachestore
options []cachestore.ClientOps // List of options
}
// clusterOptions holds the cluster configuration for Bux clusters
// at the moment we only support redis as the cluster coordinator
clusterOptions struct {
cluster.ClientInterface
options []cluster.ClientOps // List of options
}
// dataStoreOptions holds the data storage configuration and client
dataStoreOptions struct {
datastore.ClientInterface // Client for Datastore
migrationDisabled bool // If the migrations are disabled
options []datastore.ClientOps // List of options
}
// modelOptions holds the model configuration
modelOptions struct {
migrateModelNames []string // List of models for migration
migrateModels []interface{} // Models for migrations
modelNames []string // List of all models
models []interface{} // Models for use in this engine
}
// newRelicOptions holds the configuration for NewRelic
newRelicOptions struct {
app *newrelic.Application // NewRelic client application (if enabled)
enabled bool // If NewRelic is enabled for deep Transaction tracing
}
// notificationsOptions holds the configuration for notifications
notificationsOptions struct {
notifications.ClientInterface // Notifications client
options []notifications.ClientOps // List of options
webhookEndpoint string // Webhook endpoint
}
// paymailOptions holds the configuration for Paymail
paymailOptions struct {
client paymail.ClientInterface // Paymail client for communicating with Paymail providers
serverConfig *PaymailServerOptions // Server configuration if Paymail is enabled
}
// PaymailServerOptions is the options for the Paymail server
PaymailServerOptions struct {
*server.Configuration // Server configuration if Paymail is enabled
options []server.ConfigOps // Options for the paymail server
DefaultFromPaymail string // IE: [email protected]
}
// taskManagerOptions holds the configuration for taskmanager
taskManagerOptions struct {
taskmanager.TaskEngine // Client for TaskManager
options []taskmanager.TaskManagerOptions // List of options
cronCustomPeriods map[string]time.Duration // will override the default period of cronJob
}
)
// NewClient creates a new client for all bux functionality
//
// If no options are given, it will use the defaultClientOptions()
// ctx may contain a NewRelic txn (or one will be created)
func NewClient(ctx context.Context, opts ...ClientOps) (ClientInterface, error) {
// Create a new client with defaults
client := &Client{options: defaultClientOptions()}
// Overwrite defaults with any custom options provided by the user
for _, opt := range opts {
opt(client.options)
}
// Use NewRelic if it's enabled (use existing txn if found on ctx)
ctx = client.GetOrStartTxn(ctx, "new_client")
// Set the logger (if no custom logger was detected)
if client.options.logger == nil {
client.options.logger = logging.GetDefaultLogger()
}
// Load the Cachestore client
var err error
if err = client.loadCache(ctx); err != nil {
return nil, err
}
// Load the cluster coordinator
if err = client.loadCluster(ctx); err != nil {
return nil, err
}
// Load the Datastore (automatically migrate models)
if err = client.loadDatastore(ctx); err != nil {
return nil, err
}
// Run custom model datastore migrations (after initializing models)
if err = client.runModelMigrations(
client.options.models.migrateModels...,
); err != nil {
return nil, err
}
// Load the Chainstate client
if err = client.loadChainstate(ctx); err != nil {
return nil, err
}
// Load the Paymail client (if client does not exist)
if err = client.loadPaymailClient(); err != nil {
return nil, err
}
// Load the Notification client (if client does not exist)
if err = client.loadNotificationClient(); err != nil {
return nil, err
}
// Load the Taskmanager (automatically start consumers and tasks)
if err = client.loadTaskmanager(ctx); err != nil {
return nil, err
}
// Register all cron jobs
if err = client.registerCronJobs(); err != nil {
return nil, err
}
// Default paymail server config (generic capabilities and domain check disabled)
if client.options.paymail.serverConfig.Configuration == nil {
if err = client.loadDefaultPaymailConfig(); err != nil {
return nil, err
}
}
// Return the client
return client, nil
}
// AddModels will add additional models to the client
func (c *Client) AddModels(ctx context.Context, autoMigrate bool, models ...interface{}) error {
// Store the models locally in the client
c.options.addModels(modelList, models...)
// Should we migrate the models?
if autoMigrate {
// Ensure we have a datastore
d := c.Datastore()
if d == nil {
return ErrDatastoreRequired
}
// Apply the database migration with the new models
if err := d.AutoMigrateDatabase(ctx, models...); err != nil {
return err
}
// Add to the list
c.options.addModels(migrateList, models...)
// Run model migrations
if err := c.runModelMigrations(models...); err != nil {
return err
}
}
return nil
}
// Cachestore will return the Cachestore IF: exists and is enabled
func (c *Client) Cachestore() cachestore.ClientInterface {
if c.options.cacheStore != nil && c.options.cacheStore.ClientInterface != nil {
return c.options.cacheStore.ClientInterface
}
return nil
}
// Cluster will return the cluster coordinator client
func (c *Client) Cluster() cluster.ClientInterface {
if c.options.cluster != nil && c.options.cluster.ClientInterface != nil {
return c.options.cluster.ClientInterface
}
return nil
}
// Chainstate will return the Chainstate service IF: exists and is enabled
func (c *Client) Chainstate() chainstate.ClientInterface {
if c.options.chainstate != nil && c.options.chainstate.ClientInterface != nil {
return c.options.chainstate.ClientInterface
}
return nil
}
// Close will safely close any open connections (cache, datastore, etc.)
func (c *Client) Close(ctx context.Context) error {
if txn := newrelic.FromContext(ctx); txn != nil {
defer txn.StartSegment("close_all").End()
}
// Close Chainstate
ch := c.Chainstate()
if ch != nil {
ch.Close(ctx)
c.options.chainstate.ClientInterface = nil
}
// Close Datastore
ds := c.Datastore()
if ds != nil {
if err := ds.Close(ctx); err != nil {
return err
}
c.options.dataStore.ClientInterface = nil
}
// Close Taskmanager
tm := c.Taskmanager()
if tm != nil {
if err := tm.Close(ctx); err != nil {
return err
}
c.options.taskManager.TaskEngine = nil
}
return nil
}
// Datastore will return the Datastore if it exists
func (c *Client) Datastore() datastore.ClientInterface {
if c.options.dataStore != nil && c.options.dataStore.ClientInterface != nil {
return c.options.dataStore.ClientInterface
}
return nil
}
// Debug will toggle the debug mode (for all resources)
func (c *Client) Debug(on bool) {
// Set the flag on the current client
c.options.debug = on
// Set debugging on the Cachestore
if cs := c.Cachestore(); cs != nil {
cs.Debug(on)
}
// Set debugging on the Chainstate
if ch := c.Chainstate(); ch != nil {
ch.Debug(on)
}
// Set debugging on the Datastore
if ds := c.Datastore(); ds != nil {
ds.Debug(on)
}
// Set debugging on the Notifications
if n := c.Notifications(); n != nil {
n.Debug(on)
}
}
// DefaultSyncConfig will return the default sync config from the client defaults (for chainstate)
func (c *Client) DefaultSyncConfig() *SyncConfig {
return &SyncConfig{
Broadcast: c.options.chainstate.broadcasting,
BroadcastInstant: c.options.chainstate.broadcastInstant,
PaymailP2P: c.options.chainstate.paymailP2P,
SyncOnChain: c.options.chainstate.syncOnChain,
}
}
// EnableNewRelic will enable NewRelic tracing
func (c *Client) EnableNewRelic() {
if c.options.newRelic != nil && c.options.newRelic.app != nil {
c.options.newRelic.enabled = true
}
}
// GetOrStartTxn will check for an existing NewRelic transaction, if not found, it will make a new transaction
func (c *Client) GetOrStartTxn(ctx context.Context, name string) context.Context {
if c.IsNewRelicEnabled() && c.options.newRelic.app != nil {
txn := newrelic.FromContext(ctx)
if txn == nil {
txn = c.options.newRelic.app.StartTransaction(name)
}
ctx = newrelic.NewContext(ctx, txn)
}
return ctx
}
// GetModelNames will return the model names that have been loaded
func (c *Client) GetModelNames() []string {
return c.options.models.modelNames
}
// HTTPClient will return the http interface to use in the client
func (c *Client) HTTPClient() HTTPInterface {
return c.options.httpClient
}
// IsDebug will return the debug flag (bool)
func (c *Client) IsDebug() bool {
return c.options.debug
}
// IsNewRelicEnabled will return the flag (bool)
func (c *Client) IsNewRelicEnabled() bool {
return c.options.newRelic.enabled
}
// IsIUCEnabled will return the flag (bool)
func (c *Client) IsIUCEnabled() bool {
return c.options.iuc
}
// IsEncryptionKeySet will return the flag (bool) if the encryption key has been set
func (c *Client) IsEncryptionKeySet() bool {
return len(c.options.encryptionKey) > 0
}
// IsMigrationEnabled will return the flag (bool)
func (c *Client) IsMigrationEnabled() bool {
return !c.options.dataStore.migrationDisabled
}
// Logger will return the Logger if it exists
func (c *Client) Logger() *zerolog.Logger {
return c.options.logger
}
// Notifications will return the Notifications if it exists
func (c *Client) Notifications() notifications.ClientInterface {
if c.options.notifications != nil && c.options.notifications.ClientInterface != nil {
return c.options.notifications.ClientInterface
}
return nil
}
// SetNotificationsClient will overwrite the notification's client with the given client
func (c *Client) SetNotificationsClient(client notifications.ClientInterface) {
c.options.notifications.ClientInterface = client
}
// Taskmanager will return the Taskmanager if it exists
func (c *Client) Taskmanager() taskmanager.TaskEngine {
if c.options.taskManager != nil && c.options.taskManager.TaskEngine != nil {
return c.options.taskManager.TaskEngine
}
return nil
}
// UserAgent will return the user agent
func (c *Client) UserAgent() string {
return c.options.userAgent
}
// Version will return the version
func (c *Client) Version() string {
return version
}
// Metrics will return the metrics client (if it's enabled)
func (c *Client) Metrics() (metrics *metrics.Metrics, enabled bool) {
return c.options.metrics, c.options.metrics != nil
}