forked from ebhomengo/niki
1
0
Fork 0
niki/main.go

126 lines
4.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"
"fmt"
"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
// @name Authorization
// @description Type the word 'Bearer' followed by a space and Benefactor JWT token
2024-05-15 07:38:39 +00:00
// @securityDefinitions.apikey AuthBearerAdmin
// @in header
// @name Authorization
// @description Type the word 'Bearer' followed by a space and Admin JWT token.
2023-12-16 06:09:50 +00:00
func main() {
migrate := parseFlags()
cfg := config.C()
db := initDatabase(cfg, migrate)
defer func() {
if err := db.CloseStatements(); err != nil {
fmt.Printf("Error closing statements: %v\n", err)
}
}()
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),
BenefactorKindBoxVld: initial.InitBenefactorKindBoxValidator(cfg, redisAdapter, db),
AdminKindBoxReqVld: initial.InitAdminKindBoxReqValidator(db, cfg, redisAdapter),
AdminVld: initial.InitAdminValidator(db),
AdminKindBoxVld: initial.InitAdminKindBoxValidator(db, cfg),
AgentKindBoxVld: initial.InitAgentKindBoxValidator(db),
},
initial.Services{
BenefactorSvc: initial.InitBenefactorService(cfg, redisAdapter, db),
BenefactorKindBoxReqSvc: initial.InitBenefactorKindBoxReqService(db),
BenefactorAddressSvc: initial.InitBenefactorAddressService(db),
BenefactorKindBoxSvc: initial.InitBenefactorKindBoxService(db),
AdminKindBoxSvc: initial.InitAdminKindBoxService(db),
AgentKindBoxSvc: initial.InitAgentKindBoxService(db),
AdminKindBoxReqSvc: initial.InitAdminKindBoxReqService(db),
AdminSvc: initial.InitAdminService(cfg, db),
AdminReferTimeSvc: initial.InitAdminReferTimeService(db),
NotificationSvc: initial.InitNotificationService(cfg, redisAdapter, 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.BenefactorKindBoxSvc, dependencies.BenefactorKindBoxVld,
dependencies.AdminSvc, dependencies.AdminVld, dependencies.AdminAuthSvc,
2024-07-01 17:13:28 +00:00
dependencies.AdminKindBoxReqSvc, dependencies.AdminKindBoxReqVld, dependencies.AdminAuthorizationSvc,
dependencies.AdminKindBoxSvc, dependencies.AdminKindBoxVld,
dependencies.AgentKindBoxSvc, dependencies.AgentKindBoxVld, dependencies.NotificationSvc)
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
}