2023-12-16 07:20:01 +00:00
|
|
|
package main
|
2023-12-15 08:24:06 +00:00
|
|
|
|
2024-01-14 15:53:37 +00:00
|
|
|
import (
|
2024-04-28 10:39:21 +00:00
|
|
|
"flag"
|
|
|
|
|
2024-01-14 15:53:37 +00:00
|
|
|
"git.gocasts.ir/ebhomengo/niki/adapter/redis"
|
|
|
|
"git.gocasts.ir/ebhomengo/niki/config"
|
|
|
|
httpserver "git.gocasts.ir/ebhomengo/niki/delivery/http_server"
|
2024-01-28 21:39:32 +00:00
|
|
|
"git.gocasts.ir/ebhomengo/niki/internal/initial"
|
2024-01-16 16:13:06 +00:00
|
|
|
"git.gocasts.ir/ebhomengo/niki/repository/migrator"
|
2024-01-14 15:53:37 +00:00
|
|
|
"git.gocasts.ir/ebhomengo/niki/repository/mysql"
|
2024-01-15 10:33:24 +00:00
|
|
|
_ "github.com/go-sql-driver/mysql"
|
2024-01-14 15:53:37 +00:00
|
|
|
)
|
|
|
|
|
2024-01-28 21:39:32 +00:00
|
|
|
type Dependencies struct {
|
|
|
|
initial.Auth
|
|
|
|
initial.Databases
|
|
|
|
initial.Validators
|
|
|
|
initial.Services
|
|
|
|
}
|
|
|
|
|
2024-04-28 10:39:21 +00:00
|
|
|
func parseFlags() bool {
|
|
|
|
migrateFlag := flag.Bool("migrate", false, "perform database migration")
|
|
|
|
flag.Parse()
|
|
|
|
return *migrateFlag
|
|
|
|
}
|
|
|
|
|
2023-12-16 06:09:50 +00:00
|
|
|
func main() {
|
2024-04-28 10:39:21 +00:00
|
|
|
migrate := parseFlags()
|
|
|
|
|
2024-01-14 15:53:37 +00:00
|
|
|
cfg := config.C()
|
2024-04-28 10:39:21 +00:00
|
|
|
db := initDatabase(cfg, migrate)
|
2024-01-28 21:39:32 +00:00
|
|
|
redisAdapter := initRedis(cfg)
|
2024-01-14 15:53:37 +00:00
|
|
|
|
2024-01-28 21:39:32 +00:00
|
|
|
dependencies := initDependencies(cfg, redisAdapter, db)
|
2024-01-25 16:54:42 +00:00
|
|
|
|
2024-01-28 21:39:32 +00:00
|
|
|
initAndRunServer(cfg, dependencies)
|
2024-01-14 15:53:37 +00:00
|
|
|
}
|
|
|
|
|
2024-01-28 21:39:32 +00:00
|
|
|
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),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2024-01-14 15:53:37 +00:00
|
|
|
|
2024-01-28 21:39:32 +00:00
|
|
|
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)
|
2024-01-14 15:53:37 +00:00
|
|
|
|
2024-01-28 21:39:32 +00:00
|
|
|
server.Serve()
|
|
|
|
}
|
2024-01-25 11:43:39 +00:00
|
|
|
|
2024-04-28 10:39:21 +00:00
|
|
|
func initDatabase(cfg config.Config, migrate bool) *mysql.DB {
|
|
|
|
if migrate {
|
|
|
|
migrateDatabase(cfg)
|
|
|
|
}
|
2024-01-16 16:13:06 +00:00
|
|
|
|
2024-01-28 21:39:32 +00:00
|
|
|
return initial.InitMysql(cfg)
|
|
|
|
}
|
2024-01-22 08:14:57 +00:00
|
|
|
|
2024-01-28 21:39:32 +00:00
|
|
|
func initRedis(cfg config.Config) redis.Adapter {
|
|
|
|
return redis.New(cfg.Redis)
|
|
|
|
}
|
2024-01-19 16:56:11 +00:00
|
|
|
|
2024-01-28 21:39:32 +00:00
|
|
|
func migrateDatabase(cfg config.Config) {
|
|
|
|
migratorDB := migrator.New(cfg.Mysql)
|
|
|
|
migratorDB.Up()
|
2023-12-15 08:24:06 +00:00
|
|
|
}
|