From 454d3154f755235e5048fa836c55fc948eb83abc Mon Sep 17 00:00:00 2001 From: Aleksandr Soloshenko Date: Wed, 6 Dec 2023 22:49:14 +0700 Subject: [PATCH 1/3] Removed: fx usage on migrations --- internal/infra/db/cli.go | 34 +++++++++------- internal/infra/db/goose.go | 51 +++++++++++++----------- internal/infra/db/migrator.go | 15 +++---- internal/sms-gateway/models/goose.go | 16 -------- internal/sms-gateway/models/migration.go | 16 ++++---- internal/sms-gateway/models/module.go | 9 +++-- 6 files changed, 64 insertions(+), 77 deletions(-) delete mode 100644 internal/sms-gateway/models/goose.go diff --git a/internal/infra/db/cli.go b/internal/infra/db/cli.go index 93911f9e..b25ae37f 100644 --- a/internal/infra/db/cli.go +++ b/internal/infra/db/cli.go @@ -9,25 +9,22 @@ import ( type CommandMigrateParams struct { fx.In - Logger *zap.Logger - DB *gorm.DB - Migrations []Migrator `group:"migrations"` - Shut fx.Shutdowner + Logger *zap.Logger + DB *gorm.DB + Shut fx.Shutdowner } type CommandMigrate struct { - Logger *zap.Logger - DB *gorm.DB - Migrations []Migrator `group:"migrations"` - Shut fx.Shutdowner + Logger *zap.Logger + DB *gorm.DB + Shut fx.Shutdowner } func NewCommandMigrate(params CommandMigrateParams) *CommandMigrate { return &CommandMigrate{ - Logger: params.Logger, - DB: params.DB, - Migrations: params.Migrations, - Shut: params.Shut, + Logger: params.Logger, + DB: params.DB, + Shut: params.Shut, } } @@ -36,11 +33,18 @@ func (c *CommandMigrate) Cmd() string { } func (c *CommandMigrate) Run(args ...string) error { - for _, v := range c.Migrations { - if err := v.Migrate(c.DB); err != nil { - return err + err := c.DB.Transaction(func(tx *gorm.DB) error { + for _, v := range migrations { + if err := v(tx); err != nil { + return err + } } + return nil + }) + if err != nil { + return err } + c.Logger.Info("Migrations completed") return c.Shut.Shutdown() diff --git a/internal/infra/db/goose.go b/internal/infra/db/goose.go index 5e733915..c1423e1a 100644 --- a/internal/infra/db/goose.go +++ b/internal/infra/db/goose.go @@ -9,15 +9,18 @@ import ( "go.uber.org/zap" ) -type GooseStorage struct { - FS fs.FS +type GooseStorage fs.FS + +var gooseStorages = []GooseStorage{} + +func RegisterGoose(storage GooseStorage) { + gooseStorages = append(gooseStorages, storage) } type GooseMigrateParams struct { fx.In - Config Config - Storage GooseStorage + Config Config Logger *zap.Logger DB *sql.DB @@ -25,20 +28,18 @@ type GooseMigrateParams struct { } type GooseMigrate struct { - Config Config - Storage GooseStorage - DB *sql.DB - Logger *zap.Logger - Shut fx.Shutdowner + Config Config + DB *sql.DB + Logger *zap.Logger + Shut fx.Shutdowner } func NewGooseMigrate(params GooseMigrateParams) *GooseMigrate { return &GooseMigrate{ - Config: params.Config, - Logger: params.Logger, - DB: params.DB, - Storage: params.Storage, - Shut: params.Shut, + Config: params.Config, + Logger: params.Logger, + DB: params.DB, + Shut: params.Shut, } } @@ -47,8 +48,6 @@ func (c *GooseMigrate) Cmd() string { } func (c *GooseMigrate) Run(args ...string) error { - goose.SetBaseFS(c.Storage.FS) - cmd := "up" if len(args) > 0 { cmd = args[0] @@ -60,14 +59,18 @@ func (c *GooseMigrate) Run(args ...string) error { migrationsPath := "migrations/" + c.Config.Dialect - switch cmd { - case "up": - if err := goose.Up(c.DB, migrationsPath); err != nil { - return err - } - case "down": - if err := goose.Down(c.DB, migrationsPath); err != nil { - return err + for _, fs := range gooseStorages { + goose.SetBaseFS(fs) + + switch cmd { + case "up": + if err := goose.Up(c.DB, migrationsPath); err != nil { + return err + } + case "down": + if err := goose.Down(c.DB, migrationsPath); err != nil { + return err + } } } diff --git a/internal/infra/db/migrator.go b/internal/infra/db/migrator.go index a9c0ab36..ec4b8783 100644 --- a/internal/infra/db/migrator.go +++ b/internal/infra/db/migrator.go @@ -1,18 +1,13 @@ package db import ( - "go.uber.org/fx" "gorm.io/gorm" ) -type Migrator interface { - Migrate(*gorm.DB) error -} +type Migrator func(*gorm.DB) error + +var migrations = []Migrator{} -func AsMigration(f any) any { - return fx.Annotate( - f, - fx.As(new(Migrator)), - fx.ResultTags(`group:"migrations"`), - ) +func RegisterMigration(migrator Migrator) { + migrations = append(migrations, migrator) } diff --git a/internal/sms-gateway/models/goose.go b/internal/sms-gateway/models/goose.go deleted file mode 100644 index ab002cad..00000000 --- a/internal/sms-gateway/models/goose.go +++ /dev/null @@ -1,16 +0,0 @@ -package models - -import ( - "embed" - - "github.com/capcom6/sms-gateway/internal/infra/db" -) - -//go:embed migrations -var migrations embed.FS - -func GetGooseStorage() db.GooseStorage { - return db.GooseStorage{ - FS: migrations, - } -} diff --git a/internal/sms-gateway/models/migration.go b/internal/sms-gateway/models/migration.go index 34a017d1..3cb27858 100644 --- a/internal/sms-gateway/models/migration.go +++ b/internal/sms-gateway/models/migration.go @@ -1,14 +1,14 @@ package models -import "gorm.io/gorm" +import ( + "embed" -type Migration struct { -} + "gorm.io/gorm" +) -func (m Migration) Migrate(db *gorm.DB) error { - return db.AutoMigrate(&User{}, &Device{}, &Message{}, &MessageRecipient{}) -} +//go:embed migrations +var migrations embed.FS -func NewMigration() *Migration { - return &Migration{} +func Migrate(db *gorm.DB) error { + return db.AutoMigrate(&User{}, &Device{}, &Message{}, &MessageRecipient{}) } diff --git a/internal/sms-gateway/models/module.go b/internal/sms-gateway/models/module.go index 2286070e..bb9156f1 100644 --- a/internal/sms-gateway/models/module.go +++ b/internal/sms-gateway/models/module.go @@ -7,8 +7,9 @@ import ( var Module = fx.Module( "models", - fx.Provide( - db.AsMigration(NewMigration), - GetGooseStorage, - ), ) + +func init() { + db.RegisterMigration(Migrate) + db.RegisterGoose(migrations) +} From 40cd5a7c3727e8f662ea2be88834ec3be17e6299 Mon Sep 17 00:00:00 2001 From: Aleksandr Soloshenko Date: Wed, 6 Dec 2023 23:42:14 +0700 Subject: [PATCH 2/3] Improved: simplify CLI commands register --- internal/infra/cli/command.go | 17 ++++------ internal/infra/cli/module.go | 41 +++++++++++------------ internal/infra/cli/params.go | 14 -------- internal/infra/db/cli.go | 62 ++++++++++++++++++++++------------- internal/infra/db/goose.go | 41 +++++++---------------- internal/infra/db/module.go | 7 ++-- internal/infra/http/cli.go | 32 ++++-------------- internal/infra/http/module.go | 5 ++- internal/sms-gateway/app.go | 2 +- 9 files changed, 93 insertions(+), 128 deletions(-) delete mode 100644 internal/infra/cli/params.go diff --git a/internal/infra/cli/command.go b/internal/infra/cli/command.go index 5c65c240..b9e5f6a8 100644 --- a/internal/infra/cli/command.go +++ b/internal/infra/cli/command.go @@ -1,16 +1,11 @@ package cli -import "go.uber.org/fx" +type Executor any +type Args []string -type Command interface { - Cmd() string - Run(args ...string) error -} +var commands = map[string]Executor{} -func AsCommand(f any) any { - return fx.Annotate( - f, - fx.As(new(Command)), - fx.ResultTags(`group:"commands"`), - ) +// Регистрирует консольную команду cmd с испольнителем executor +func Register(cmd string, executor Executor) { + commands[cmd] = executor } diff --git a/internal/infra/cli/module.go b/internal/infra/cli/module.go index 7c4e98ba..4fdd42f6 100644 --- a/internal/infra/cli/module.go +++ b/internal/infra/cli/module.go @@ -9,27 +9,24 @@ import ( var DefaultCommand = "" -var Module = fx.Module( - "cli", - fx.Decorate(func(log *zap.Logger) *zap.Logger { - return log.Named("cli") - }), - fx.Invoke(func(params Params) error { - cmd := DefaultCommand - args := []string{} - if len(os.Args) > 1 { - cmd = os.Args[1] - args = os.Args[2:] - } +func GetModule() fx.Option { + cmd := DefaultCommand + args := []string{} + if len(os.Args) > 1 { + cmd = os.Args[1] + } - for _, v := range params.Commands { - if v.Cmd() != cmd { - continue - } + executor, ok := commands[cmd] + if !ok { + return fx.Invoke(func(logger *zap.Logger, shut fx.Shutdowner) error { + logger.Error("Command is not supported", zap.String("cmd", cmd)) + return shut.Shutdown() + }) + } - return v.Run(args...) - } - params.Logger.Info("Command is not supported", zap.String("command", cmd)) - return params.Shut.Shutdown() - }), -) + return fx.Module( + "cli", + fx.Supply(Args(args)), + fx.Invoke(executor), + ) +} diff --git a/internal/infra/cli/params.go b/internal/infra/cli/params.go deleted file mode 100644 index bfa75540..00000000 --- a/internal/infra/cli/params.go +++ /dev/null @@ -1,14 +0,0 @@ -package cli - -import ( - "go.uber.org/fx" - "go.uber.org/zap" -) - -type Params struct { - fx.In - - Logger *zap.Logger - Commands []Command `group:"commands"` - Shut fx.Shutdowner -} diff --git a/internal/infra/db/cli.go b/internal/infra/db/cli.go index b25ae37f..bc4d1d98 100644 --- a/internal/infra/db/cli.go +++ b/internal/infra/db/cli.go @@ -14,26 +14,8 @@ type CommandMigrateParams struct { Shut fx.Shutdowner } -type CommandMigrate struct { - Logger *zap.Logger - DB *gorm.DB - Shut fx.Shutdowner -} - -func NewCommandMigrate(params CommandMigrateParams) *CommandMigrate { - return &CommandMigrate{ - Logger: params.Logger, - DB: params.DB, - Shut: params.Shut, - } -} - -func (c *CommandMigrate) Cmd() string { - return "db:auto-migrate" -} - -func (c *CommandMigrate) Run(args ...string) error { - err := c.DB.Transaction(func(tx *gorm.DB) error { +func AutoMigrate(params CommandMigrateParams) error { + err := params.DB.Transaction(func(tx *gorm.DB) error { for _, v := range migrations { if err := v(tx); err != nil { return err @@ -45,7 +27,43 @@ func (c *CommandMigrate) Run(args ...string) error { return err } - c.Logger.Info("Migrations completed") + params.Logger.Info("Migrations completed") - return c.Shut.Shutdown() + return params.Shut.Shutdown() } + +// type CommandMigrate struct { +// Logger *zap.Logger +// DB *gorm.DB +// Shut fx.Shutdowner +// } + +// func NewCommandMigrate(params CommandMigrateParams) *CommandMigrate { +// return &CommandMigrate{ +// Logger: params.Logger, +// DB: params.DB, +// Shut: params.Shut, +// } +// } + +// func (c *CommandMigrate) Cmd() string { +// return "db:auto-migrate" +// } + +// func (c *CommandMigrate) Run(args ...string) error { +// err := c.DB.Transaction(func(tx *gorm.DB) error { +// for _, v := range migrations { +// if err := v(tx); err != nil { +// return err +// } +// } +// return nil +// }) +// if err != nil { +// return err +// } + +// c.Logger.Info("Migrations completed") + +// return c.Shut.Shutdown() +// } diff --git a/internal/infra/db/goose.go b/internal/infra/db/goose.go index c1423e1a..35b30414 100644 --- a/internal/infra/db/goose.go +++ b/internal/infra/db/goose.go @@ -4,6 +4,7 @@ import ( "database/sql" "io/fs" + "github.com/capcom6/sms-gateway/internal/infra/cli" "github.com/pressly/goose/v3" "go.uber.org/fx" "go.uber.org/zap" @@ -20,6 +21,8 @@ func RegisterGoose(storage GooseStorage) { type GooseMigrateParams struct { fx.In + Args cli.Args + Config Config Logger *zap.Logger @@ -27,54 +30,34 @@ type GooseMigrateParams struct { Shut fx.Shutdowner } -type GooseMigrate struct { - Config Config - DB *sql.DB - Logger *zap.Logger - Shut fx.Shutdowner -} - -func NewGooseMigrate(params GooseMigrateParams) *GooseMigrate { - return &GooseMigrate{ - Config: params.Config, - Logger: params.Logger, - DB: params.DB, - Shut: params.Shut, - } -} - -func (c *GooseMigrate) Cmd() string { - return "db:migrate" -} - -func (c *GooseMigrate) Run(args ...string) error { +func Migrate(params GooseMigrateParams) error { cmd := "up" - if len(args) > 0 { - cmd = args[0] + if len(params.Args) > 0 { + cmd = params.Args[0] } - if err := goose.SetDialect(c.Config.Dialect); err != nil { + if err := goose.SetDialect(params.Config.Dialect); err != nil { return err } - migrationsPath := "migrations/" + c.Config.Dialect + migrationsPath := "migrations/" + params.Config.Dialect for _, fs := range gooseStorages { goose.SetBaseFS(fs) switch cmd { case "up": - if err := goose.Up(c.DB, migrationsPath); err != nil { + if err := goose.Up(params.DB, migrationsPath); err != nil { return err } case "down": - if err := goose.Down(c.DB, migrationsPath); err != nil { + if err := goose.Down(params.DB, migrationsPath); err != nil { return err } } } - c.Logger.Info("Migrations completed") + params.Logger.Info("Migrations completed") - return c.Shut.Shutdown() + return params.Shut.Shutdown() } diff --git a/internal/infra/db/module.go b/internal/infra/db/module.go index 5b9e6614..9e7762d0 100644 --- a/internal/infra/db/module.go +++ b/internal/infra/db/module.go @@ -14,7 +14,10 @@ var Module = fx.Module( fx.Provide( New, NewSQL, - cli.AsCommand(NewCommandMigrate), - cli.AsCommand(NewGooseMigrate), ), ) + +func init() { + cli.Register("db:auto-migrate", AutoMigrate) + cli.Register("db:migrate", Migrate) +} diff --git a/internal/infra/http/cli.go b/internal/infra/http/cli.go index 89bbaaa4..4621b94b 100644 --- a/internal/infra/http/cli.go +++ b/internal/infra/http/cli.go @@ -17,39 +17,19 @@ type RunServerParams struct { LC fx.Lifecycle } -type RunServer struct { - Config Config - App *fiber.App - Logger *zap.Logger - LC fx.Lifecycle -} - -func NewRunServer(params RunServerParams) *RunServer { - return &RunServer{ - Config: configDefault(params.Config), - App: params.App, - Logger: params.Logger, - LC: params.LC, - } -} - -func (c *RunServer) Cmd() string { - return "http:run" -} - -func (c *RunServer) Run(args ...string) error { +func Run(params RunServerParams) error { go func() { - c.Logger.Info("Starting server...") + params.Logger.Info("Starting server...") - err := c.App.Listen(c.Config.Listen) + err := params.App.Listen(params.Config.Listen) if err != nil { - c.Logger.Error("Error starting server", zap.Error(err)) + params.Logger.Error("Error starting server", zap.Error(err)) } }() - c.LC.Append(fx.Hook{ + params.LC.Append(fx.Hook{ OnStop: func(ctx context.Context) error { - return c.App.ShutdownWithContext(ctx) + return params.App.ShutdownWithContext(ctx) }, }) diff --git a/internal/infra/http/module.go b/internal/infra/http/module.go index 190b6ef2..1edb15a6 100644 --- a/internal/infra/http/module.go +++ b/internal/infra/http/module.go @@ -13,6 +13,9 @@ var Module = fx.Module( }), fx.Provide( New, - cli.AsCommand(NewRunServer), ), ) + +func init() { + cli.Register("http:run", Run) +} diff --git a/internal/sms-gateway/app.go b/internal/sms-gateway/app.go index 0b42c6a1..15585b83 100644 --- a/internal/sms-gateway/app.go +++ b/internal/sms-gateway/app.go @@ -20,7 +20,6 @@ import ( var Module = fx.Module( "server", logger.Module, - cli.Module, appconfig.Module, http.Module, validator.Module, @@ -34,6 +33,7 @@ var Module = fx.Module( func Run() { cli.DefaultCommand = "http:run" fx.New( + cli.GetModule(), Module, fx.WithLogger(func(logger *zap.Logger) fxevent.Logger { logOption := fxevent.ZapLogger{Logger: logger} From 7833a462e7ee7c9d79967224e9b084dbf116788b Mon Sep 17 00:00:00 2001 From: Aleksandr Soloshenko Date: Fri, 8 Dec 2023 16:23:36 +0700 Subject: [PATCH 3/3] Refactored: moved `crypto` package from `infra` --- internal/infra/db/cli.go | 36 --------------------- internal/sms-gateway/services/auth.go | 2 +- {internal/infra => pkg}/crypto/passwords.go | 0 3 files changed, 1 insertion(+), 37 deletions(-) rename {internal/infra => pkg}/crypto/passwords.go (100%) diff --git a/internal/infra/db/cli.go b/internal/infra/db/cli.go index bc4d1d98..d4299846 100644 --- a/internal/infra/db/cli.go +++ b/internal/infra/db/cli.go @@ -31,39 +31,3 @@ func AutoMigrate(params CommandMigrateParams) error { return params.Shut.Shutdown() } - -// type CommandMigrate struct { -// Logger *zap.Logger -// DB *gorm.DB -// Shut fx.Shutdowner -// } - -// func NewCommandMigrate(params CommandMigrateParams) *CommandMigrate { -// return &CommandMigrate{ -// Logger: params.Logger, -// DB: params.DB, -// Shut: params.Shut, -// } -// } - -// func (c *CommandMigrate) Cmd() string { -// return "db:auto-migrate" -// } - -// func (c *CommandMigrate) Run(args ...string) error { -// err := c.DB.Transaction(func(tx *gorm.DB) error { -// for _, v := range migrations { -// if err := v(tx); err != nil { -// return err -// } -// } -// return nil -// }) -// if err != nil { -// return err -// } - -// c.Logger.Info("Migrations completed") - -// return c.Shut.Shutdown() -// } diff --git a/internal/sms-gateway/services/auth.go b/internal/sms-gateway/services/auth.go index bbaaa26d..d8e095f2 100644 --- a/internal/sms-gateway/services/auth.go +++ b/internal/sms-gateway/services/auth.go @@ -3,9 +3,9 @@ package services import ( "fmt" - "github.com/capcom6/sms-gateway/internal/infra/crypto" "github.com/capcom6/sms-gateway/internal/sms-gateway/models" "github.com/capcom6/sms-gateway/internal/sms-gateway/repositories" + "github.com/capcom6/sms-gateway/pkg/crypto" "github.com/jaevor/go-nanoid" ) diff --git a/internal/infra/crypto/passwords.go b/pkg/crypto/passwords.go similarity index 100% rename from internal/infra/crypto/passwords.go rename to pkg/crypto/passwords.go