niki/main.go

107 lines
3.3 KiB
Go
Raw Normal View History

2023-12-16 07:20:01 +00:00
package main
2023-12-15 08:24:06 +00:00
import (
"flag"
"git.gocasts.ir/ebhomengo/niki/adapter/redis"
"git.gocasts.ir/ebhomengo/niki/config"
httpserver "git.gocasts.ir/ebhomengo/niki/delivery/http_server"
"git.gocasts.ir/ebhomengo/niki/internal/initial"
"git.gocasts.ir/ebhomengo/niki/repository/migrator"
"git.gocasts.ir/ebhomengo/niki/repository/mysql"
_ "github.com/go-sql-driver/mysql"
)
type Dependencies struct {
initial.Auth
initial.Databases
initial.Validators
initial.Services
initial.AdminAuthorization
}
func parseFlags() bool {
migrateFlag := flag.Bool("migrate", false, "perform database migration")
flag.Parse()
return *migrateFlag
}
2024-05-15 07:38:39 +00:00
// @securityDefinitions.apikey AuthBearerBenefactor
2024-05-14 13:07:09 +00:00
// @in header
2024-05-15 07:38:39 +00:00
// @name Authorization Benefactor
// @securityDefinitions.apikey AuthBearerAdmin
// @in header
// @name Authorization admin
2023-12-16 06:09:50 +00:00
func main() {
migrate := parseFlags()
cfg := config.C()
db := initDatabase(cfg, migrate)
redisAdapter := initRedis(cfg)
dependencies := initDependencies(cfg, redisAdapter, db)
2024-01-25 16:54:42 +00:00
initAndRunServer(cfg, dependencies)
}
func initDependencies(cfg config.Config, redisAdapter redis.Adapter, db *mysql.DB) *Dependencies {
return &Dependencies{
initial.Auth{
BenefactorAuthSvc: initial.InitBenefactorAuthService(cfg),
AdminAuthSvc: initial.InitAdminAuthService(cfg),
},
initial.Databases{
BenefactorAddressDB: initial.InitBenefactorAddressDB(db),
BenefactorKindBoxReqDB: initial.InitBenefactorKindBoxReqDB(db),
KindBoxRepo: initial.InitKindBoxRepo(db),
AdminMysql: initial.InitAdminMysql(db),
},
initial.Validators{
BenefactorVld: initial.InitBenefactorValidator(),
BenefactorKindBoxReqVld: initial.InitBenefactorKindBoxReqValidator(cfg, redisAdapter, db),
BenefactorAddressVld: initial.InitBenefactorAddressValidator(cfg, redisAdapter, db),
AdminKindBoxReqVld: initial.InitAdminKindBoxReqValidator(db),
AdminVld: initial.InitAdminValidator(db),
},
initial.Services{
BenefactorSvc: initial.InitBenefactorService(cfg, redisAdapter, db),
BenefactorKindBoxReqSvc: initial.InitBenefactorKindBoxReqService(db),
BenefactorAddressSvc: initial.InitBenefactorAddressService(db),
AdminKindBoxSvc: initial.InitAdminKindBoxService(db),
AdminKindBoxReqSvc: initial.InitAdminKindBoxReqService(db),
AdminSvc: initial.InitAdminService(cfg, db),
},
initial.AdminAuthorization{
AdminAuthorizationSvc: initial.InitAdminAuthorizationService(db),
},
}
}
func initAndRunServer(cfg config.Config, dependencies *Dependencies) {
server := httpserver.New(cfg,
dependencies.BenefactorSvc, dependencies.BenefactorVld, dependencies.BenefactorAuthSvc,
dependencies.BenefactorKindBoxReqSvc, dependencies.BenefactorKindBoxReqVld,
dependencies.BenefactorAddressSvc, dependencies.BenefactorAddressVld,
dependencies.AdminSvc, dependencies.AdminVld, dependencies.AdminAuthSvc,
dependencies.AdminKindBoxReqSvc, dependencies.AdminKindBoxReqVld, dependencies.AdminAuthorizationSvc)
server.Serve()
}
func initDatabase(cfg config.Config, migrate bool) *mysql.DB {
if migrate {
migrateDatabase(cfg)
}
return initial.InitMysql(cfg)
}
func initRedis(cfg config.Config) redis.Adapter {
return redis.New(cfg.Redis)
}
func migrateDatabase(cfg config.Config) {
migratorDB := migrator.New(cfg.Mysql)
migratorDB.Up()
2023-12-15 08:24:06 +00:00
}