Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions internal/infra/cli/command.go
Original file line number Diff line number Diff line change
@@ -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
}
41 changes: 19 additions & 22 deletions internal/infra/cli/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
}
14 changes: 0 additions & 14 deletions internal/infra/cli/params.go

This file was deleted.

46 changes: 16 additions & 30 deletions internal/infra/db/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,25 @@ 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
}

func NewCommandMigrate(params CommandMigrateParams) *CommandMigrate {
return &CommandMigrate{
Logger: params.Logger,
DB: params.DB,
Migrations: params.Migrations,
Shut: params.Shut,
}
}

func (c *CommandMigrate) Cmd() string {
return "db:auto-migrate"
}

func (c *CommandMigrate) Run(args ...string) error {
for _, v := range c.Migrations {
if err := v.Migrate(c.DB); err != nil {
return err
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
}
}
return nil
})
if err != nil {
return err
}
c.Logger.Info("Migrations completed")

return c.Shut.Shutdown()
params.Logger.Info("Migrations completed")

return params.Shut.Shutdown()
}
72 changes: 29 additions & 43 deletions internal/infra/db/goose.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,74 +4,60 @@ 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"
)

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
Args cli.Args

Config Config

Logger *zap.Logger
DB *sql.DB
Shut fx.Shutdowner
}

type GooseMigrate struct {
Config Config
Storage GooseStorage
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,
}
}

func (c *GooseMigrate) Cmd() string {
return "db:migrate"
}

func (c *GooseMigrate) Run(args ...string) error {
goose.SetBaseFS(c.Storage.FS)

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

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(params.DB, migrationsPath); err != nil {
return err
}
case "down":
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()
}
15 changes: 5 additions & 10 deletions internal/infra/db/migrator.go
Original file line number Diff line number Diff line change
@@ -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)
}
7 changes: 5 additions & 2 deletions internal/infra/db/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
32 changes: 6 additions & 26 deletions internal/infra/http/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
})

Expand Down
5 changes: 4 additions & 1 deletion internal/infra/http/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ var Module = fx.Module(
}),
fx.Provide(
New,
cli.AsCommand(NewRunServer),
),
)

func init() {
cli.Register("http:run", Run)
}
2 changes: 1 addition & 1 deletion internal/sms-gateway/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
var Module = fx.Module(
"server",
logger.Module,
cli.Module,
appconfig.Module,
http.Module,
validator.Module,
Expand All @@ -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}
Expand Down
16 changes: 0 additions & 16 deletions internal/sms-gateway/models/goose.go

This file was deleted.

16 changes: 8 additions & 8 deletions internal/sms-gateway/models/migration.go
Original file line number Diff line number Diff line change
@@ -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{})
}
Loading