-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
563 lines (514 loc) · 19 KB
/
base.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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
package registry
import (
"context"
"fmt"
"os"
"os/signal"
"reflect"
"strings"
"time"
"github.com/driver005/gateway/config"
"github.com/driver005/gateway/interfaces"
"github.com/driver005/gateway/middlewares"
"github.com/driver005/gateway/migrations"
"github.com/driver005/gateway/repository"
"github.com/driver005/gateway/routes"
"github.com/driver005/gateway/routes/admin"
"github.com/driver005/gateway/routes/store"
"github.com/driver005/gateway/services"
"github.com/driver005/gateway/sql"
"github.com/driver005/gateway/strategies"
"github.com/driver005/gateway/utils"
"github.com/go-playground/validator/v10"
"github.com/sarulabs/di"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/driver/sqlserver"
"gorm.io/gorm"
dbLogger "gorm.io/gorm/logger"
"gorm.io/gorm/schema"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/session"
"github.com/gofiber/fiber/v3/middleware/favicon"
_ "github.com/lib/pq" // FIX: https://stackoverflow.com/questions/52789531/how-do-i-solve-panic-sql-unknown-driver-postgres-forgotten-import
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
)
type Base struct {
logger *zap.SugaredLogger
buildVersion string
buildHash string
buildDate string
trc trace.Tracer
database sql.Database
container di.Container
config *config.Config
session *session.Store
middleware *middlewares.Handler
adminRouter fiber.Router
storeRouter fiber.Router
router *fiber.App
routes *routes.Routes
migrations *migrations.Handler
validator *validator.Validate
//Interfaces
priceSelectionStrategy interfaces.IPriceSelectionStrategy
taxCalculationStrategy interfaces.ITaxCalculationStrategy
inventoryService interfaces.IInventoryService
stockLocationService interfaces.IStockLocationService
cacheService interfaces.ICacheService
pricingModuleService interfaces.IPricingModuleService
fileService interfaces.IFileService
batchJobStrategy interfaces.IBatchJobStrategy
cartCompletionStrategy interfaces.ICartCompletionStrategy
//Repository
addressRepo *repository.AddressRepo
analyticsConfigRepo *repository.AnalyticsConfigRepo
batchJobRepo *repository.BatchJobRepo
cartRepo *repository.CartRepo
claimImageRepo *repository.ClaimImageRepo
claimItemRepo *repository.ClaimItemRepo
claimTagRepo *repository.ClaimTagRepo
claimRepo *repository.ClaimRepo
countryRepo *repository.CountryRepo
currencyRepo *repository.CurrencyRepo
customShippingOptionRepo *repository.CustomShippingOptionRepo
customerGroupRepo *repository.CustomerGroupRepo
customerRepo *repository.CustomerRepo
discountConditionRepo *repository.DiscountConditionRepo
discountRuleRepo *repository.DiscountRuleRepo
discountRepo *repository.DiscountRepo
draftOrderRepo *repository.DraftOrderRepo
fulfillmentProviderRepo *repository.FulfillmentProviderRepo
fulfillmentRepo *repository.FulfillmentRepo
giftCardTransactionRepo *repository.GiftCardTransactionRepo
giftCardRepo *repository.GiftCardRepo
idempotencyKeyRepo *repository.IdempotencyKeyRepo
imageRepo *repository.ImageRepo
inviteRepo *repository.InviteRepo
ineItemAdjustmentRepo *repository.LineItemAdjustmentRepo
ineItemTaxLineRepo *repository.LineItemTaxLineRepo
ineItemRepo *repository.LineItemRepo
moneyAmountRepo *repository.MoneyAmountRepo
noteRepo *repository.NoteRepo
notificationProviderRepo *repository.NotificationProviderRepo
notificationRepo *repository.NotificationRepo
oAuthRepo *repository.OAuthRepo
orderEditRepo *repository.OrderEditRepo
orderItemChangeRepo *repository.OrderItemChangeRepo
orderRepo *repository.OrderRepo
paymentCollectionRepo *repository.PaymentCollectionRepo
paymentProviderRepo *repository.PaymentProviderRepo
paymentSessionRepo *repository.PaymentSessionRepo
paymentRepo *repository.PaymentRepo
priceListRepo *repository.PriceListRepo
productCategoryRepo *repository.ProductCategoryRepo
productCollectionRepo *repository.ProductCollectionRepo
productOptionValueRepo *repository.ProductOptionValueRepo
productOptionRepo *repository.ProductOptionRepo
productTagRepo *repository.ProductTagRepo
productTaxRateRepo *repository.ProductTaxRateRepo
productTypeRepo *repository.ProductTypeRepo
productVariantInventoryItem *repository.ProductVariantInventoryItem
productVariantRepo *repository.ProductVariantRepo
productRepo *repository.ProductRepo
publishableApiKeySalesChannelRepo *repository.PublishableApiKeySalesChannelRepo
publishableApiKeyRepo *repository.PublishableApiKeyRepo
refundRepo *repository.RefundRepo
regionRepo *repository.RegionRepo
returnItemRepo *repository.ReturnItemRepo
returnReasonRepo *repository.ReturnReasonRepo
returnRepo *repository.ReturnRepo
salesChannelLocationRepo *repository.SalesChannelLocationRepo
salesChannelRepo *repository.SalesChannelRepo
shippingMethodTaxLineRepo *repository.ShippingMethodTaxLineRepo
shippingMethodRepo *repository.ShippingMethodRepo
shippingOptionRequirementRepo *repository.ShippingOptionRequirementRepo
shippingOptionRepo *repository.ShippingOptionRepo
shippingProfileRepo *repository.ShippingProfileRepo
shippingTaxRateRepo *repository.ShippingTaxRateRepo
stagedJobRepo *repository.StagedJobRepo
storeRepo *repository.StoreRepo
swapRepo *repository.SwapRepo
taxProviderRepo *repository.TaxProviderRepo
taxRateRepo *repository.TaxRateRepo
trackingLinkRepo *repository.TrackingLinkRepo
userRepo *repository.UserRepo
//Services
analyticsConfigService *services.AnalyticsConfigService
authService *services.AuthService
batchJobService *services.BatchJobService
cartService *services.CartService
claimItemService *services.ClaimItemService
claimService *services.ClaimService
csvParserService *services.CsvParserService
currencyService *services.CurrencyService
customShippingOptionService *services.CustomShippingOptionService
customerGroupService *services.CustomerGroupService
customerService *services.CustomerService
discountConditionService *services.DiscountConditionService
discountService *services.DiscountService
draftOrderService *services.DraftOrderService
eventBusService *services.Bus
defaultFileService *services.DefaultFileService
fulfillmentProviderService *services.FulfillmentProviderService
fulfillmentService *services.FulfillmentService
giftCardService *services.GiftCardService
idempotencyKeyService *services.IdempotencyKeyService
inviteService *services.InviteService
lineItemAdjustmentService *services.LineItemAdjustmentService
lineItemService *services.LineItemService
newTotalsService *services.NewTotalsService
noteService *services.NoteService
notificationService *services.NotificationService
oAuthService *services.OAuthService
orderItemChangeService *services.OrderItemChangeService
orderEditService *services.OrderEditService
orderService *services.OrderService
paymentCollectionService *services.PaymentCollectionService
paymentProviderService *services.PaymentProviderService
paymentService *services.PaymentService
priceListService *services.PriceListService
pricingService *services.PricingService
productCategoryService *services.ProductCategoryService
productCollectionService *services.ProductCollectionService
productTagService *services.ProductTagService
productTaxRateService *services.ProductTaxRateService
productTypeService *services.ProductTypeService
productVariantInventoryService *services.ProductVariantInventoryService
productVariantService *services.ProductVariantService
productService *services.ProductService
publishableApiKeyService *services.PublishableApiKeyService
regionService *services.RegionService
returnReasonService *services.ReturnReasonService
returnedService *services.ReturnService
salesChannelInventoryService *services.SalesChannelInventoryService
salesChannelLocationService *services.SalesChannelLocationService
salesChannelService *services.SalesChannelService
defaultSearchService *services.DefaultSearchService
shippingOptionService *services.ShippingOptionService
shippingProfileService *services.ShippingProfileService
shippingTaxRateService *services.ShippingTaxRateService
stagedJobService *services.StagedJobService
storeService *services.StoreService
strategyResolverService *services.StrategyResolverService
swapService *services.SwapService
systemProviderService *services.SystemProviderService
systemTaxService *services.SystemTaxService
taxProviderService *services.TaxProviderService
taxRateService *services.TaxRateService
tockenService *services.TockenService
totalsService *services.TotalsService
userService *services.UserService
flagRouter *services.FlagRouter
//Routes
//Admin
adminAnalyticsConfig *admin.AnalyticsConfig
adminApp *admin.App
adminAuth *admin.Auth
adminBatch *admin.Batch
adminCollection *admin.Collection
adminCurrencie *admin.Currencie
adminCustomerGroup *admin.CustomerGroup
adminCustomer *admin.Customer
adminDiscount *admin.Discount
adminDraftOrder *admin.DraftOrder
adminGiftCard *admin.GiftCard
adminInventoryItem *admin.InventoryItem
adminInvite *admin.Invite
adminNote *admin.Note
adminNotification *admin.Notification
adminOrderEdit *admin.OrderEdit
adminOrder *admin.Order
adminPaymentCollection *admin.PaymentCollection
adminPayment *admin.Payment
adminPriceList *admin.PriceList
adminProductCategory *admin.ProductCategory
adminProductTag *admin.ProductTag
adminProductType *admin.ProductType
adminProduct *admin.Product
adminPublishableApiKey *admin.PublishableApiKey
adminRegion *admin.Region
adminReservation *admin.Reservation
adminReturnReason *admin.ReturnReason
adminReturn *admin.Return
adminSalesChannel *admin.SalesChannel
adminShippingOption *admin.ShippingOption
adminShippingProfile *admin.ShippingProfile
adminStockLocation *admin.StockLocation
adminStore *admin.Store
adminSwap *admin.Swap
adminTaxRate *admin.TaxRate
adminUpload *admin.Upload
adminUser *admin.User
adminVariant *admin.Variant
//Store
storeAuth *store.Auth
storeCart *store.Cart
storeCollection *store.Collection
storeCustomer *store.Customer
storeGiftCard *store.GiftCard
storeOrder *store.Order
storeOrderEdit *store.OrderEdit
storePaymentCollection *store.PaymentCollection
storeProduct *store.Product
storeProductCategory *store.ProductCategory
storeProductTag *store.ProductTag
storeProductType *store.ProductType
storeRegion *store.Region
storeReturn *store.Return
storeReturnReason *store.ReturnReason
storeShippingOption *store.ShippingOption
storeSwap *store.Swap
storeVariant *store.Variant
}
func NewRegistry() *Base {
r := new(Base)
r.Config()
return r
}
func (m *Base) WithBuildInfo(version, hash, date string) *Base {
m.buildVersion = version
m.buildHash = hash
m.buildDate = date
return m
}
func (m *Base) BuildVersion() string {
return m.buildVersion
}
func (m *Base) BuildDate() string {
return m.buildDate
}
func (m *Base) BuildHash() string {
return m.buildHash
}
func (m *Base) Logger() *zap.SugaredLogger {
if m.logger == nil {
level, err := zap.ParseAtomicLevel(m.Config().Logger.Level)
if err != nil {
panic(err)
}
var encoderConfig zapcore.EncoderConfig
if m.Config().Logger.Development {
encoderConfig = zap.NewDevelopmentEncoderConfig()
} else {
encoderConfig = zap.NewProductionEncoderConfig()
}
config := zap.Config{
Level: level,
Development: m.Config().Logger.Development,
Encoding: m.Config().Logger.Encoding,
EncoderConfig: encoderConfig,
Sampling: &zap.SamplingConfig{
Initial: 100,
Thereafter: 100,
},
OutputPaths: []string{
"stderr",
},
ErrorOutputPaths: []string{
"stderr",
},
InitialFields: map[string]interface{}{
"pid": os.Getpid(),
},
}
m.logger = zap.Must(config.Build()).Sugar()
defer m.logger.Sync()
}
return m.logger
}
func (m *Base) Config() *config.Config {
if m.config == nil {
con, err := config.LoadConfig("./config")
if err != nil {
m.Logger().Fatal("Unable to create config service")
}
m.config = con
}
return m.config
}
func (m *Base) Validator() *validator.Validate {
if m.validator == nil {
validate := validator.New()
// register function to get tag name from json tags.
validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
if name == "-" {
return ""
}
return name
})
m.validator = validate
}
return m.validator
}
func (m *Base) Tracer(ctx context.Context) trace.Tracer {
if m.trc == nil {
tp := otel.GetTracerProvider()
m.trc = tp.Tracer("github.com/driver005/gateway", trace.WithInstrumentationVersion(m.BuildVersion()))
}
return m.trc
}
func (m *Base) Database() sql.Database {
return m.database
}
func (m *Base) Container() di.Container {
return m.container
}
func (m *Base) GetDialector(database config.Database) gorm.Dialector {
var credentials string
if len(database.Username) > 0 {
credentials = database.Username
if len(database.Username) > 0 {
credentials += fmt.Sprintf(":%s", database.Password)
}
credentials += "@"
}
switch database.Type {
case "mysql":
return mysql.New(mysql.Config{
DriverName: database.Type,
DSN: fmt.Sprintf(
"%stcp(%s:%d)/%s",
credentials,
database.Host,
database.Port,
database.DBname,
),
})
case "pgx", "pq", "postgres", "cockroach":
return postgres.New(postgres.Config{
DriverName: database.Type,
DSN: fmt.Sprintf(
"postgres://%s%s:%d/%s",
credentials,
database.Host,
database.Port,
database.DBname,
),
})
case "sqlite":
return sqlite.Open(
database.DBname,
)
case "sqlserver":
return sqlserver.New(sqlserver.Config{
DriverName: database.Type,
DSN: fmt.Sprintf(
"sqlserver://%s%s:%d?database=%s",
credentials,
database.Host,
database.Port,
database.DBname,
),
})
default:
m.Logger().Fatal("Unknown database driver, please change the config file")
return nil
}
}
func (m *Base) Init(ctx context.Context) error {
if m.database == nil {
// new db connection
database, err := gorm.Open(m.GetDialector(m.Config().MasterDatabase), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
SingularTable: true,
},
DisableForeignKeyConstraintWhenMigrating: true,
Logger: dbLogger.Default.LogMode(dbLogger.Info),
})
if err != nil {
return err
}
m.database, err = sql.NewManager(database, m.logger)
if err != nil {
return err
}
}
return nil
}
func (m *Base) Context() *gorm.DB {
return m.database.DB(context.Background())
}
func (m *Base) Manager(ctx context.Context) *gorm.DB {
return m.database.DB(ctx)
}
func (m *Base) Migration() *migrations.Handler {
if m.migrations == nil {
m.migrations = migrations.New(m)
}
return m.migrations
}
func (m *Base) Session() *session.Store {
if m.session == nil {
m.session = session.New()
}
return m.session
}
func (m *Base) Middleware() *middlewares.Handler {
if m.middleware == nil {
m.middleware = middlewares.New(m)
}
return m.middleware
}
func (m *Base) NewRouter(app *fiber.App) *fiber.App {
if m.router == nil {
m.router = app
}
return m.router
}
func (m *Base) AdminRouter() fiber.Router {
if m.adminRouter == nil {
m.adminRouter = m.router.Group("/admin")
}
return m.adminRouter
}
func (m *Base) StoreRouter() fiber.Router {
if m.storeRouter == nil {
m.storeRouter = m.router.Group("/store")
}
return m.storeRouter
}
func (m *Base) Routes() *routes.Routes {
if m.routes == nil {
m.routes = routes.New(m)
}
return m.routes
}
func (m *Base) AddRoutes(router *fiber.App) {
m.NewRouter(router)
m.Routes().SetRoutes()
}
func (m *Base) Strategies() {
m.cartCompletionStrategy = strategies.NewCartCompletionStrategy(m)
m.priceSelectionStrategy = strategies.NewPriceSelectionStrategy(m)
m.taxCalculationStrategy = strategies.NewTaxCalculationStrategy(m)
}
func (m *Base) Setup() {
app := fiber.New(fiber.Config{
ServerHeader: m.Config().Server.ServerName,
AppName: m.Config().Server.ServerName,
WriteTimeout: time.Duration(m.Config().Server.Timeout) * time.Second,
ReadTimeout: time.Duration(m.Config().Server.Timeout) * time.Second,
StrictRouting: true,
ReadBufferSize: m.Config().Server.RequestBodyLimit,
ErrorHandler: utils.ErrorHandler,
})
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
//lint:ignore S1005 unnecessary assignment to the blank identifier
_ = <-c
fmt.Println("\nGracefully shutting down...")
_ = app.Shutdown()
}()
app.Use(favicon.New())
m.AddRoutes(app)
if err := app.Listen(fmt.Sprintf("%s:%d", m.Config().Server.Host, m.Config().Server.Port)); err != nil {
m.Logger().Fatal(err)
}
}