forked from ebhomengo/niki
1
0
Fork 0

feat(niki): add migrate flag to main for running migrations

This commit is contained in:
Erfan Mohammadi 2024-04-28 14:09:21 +03:30
parent 659cba01d3
commit 02c16595b1
1 changed files with 15 additions and 3 deletions

16
main.go
View File

@ -1,6 +1,8 @@
package main
import (
"flag"
"git.gocasts.ir/ebhomengo/niki/adapter/redis"
"git.gocasts.ir/ebhomengo/niki/config"
httpserver "git.gocasts.ir/ebhomengo/niki/delivery/http_server"
@ -17,9 +19,17 @@ type Dependencies struct {
initial.Services
}
func parseFlags() bool {
migrateFlag := flag.Bool("migrate", false, "perform database migration")
flag.Parse()
return *migrateFlag
}
func main() {
migrate := parseFlags()
cfg := config.C()
db := initDatabase(cfg)
db := initDatabase(cfg, migrate)
redisAdapter := initRedis(cfg)
dependencies := initDependencies(cfg, redisAdapter, db)
@ -68,8 +78,10 @@ func initAndRunServer(cfg config.Config, dependencies *Dependencies) {
server.Serve()
}
func initDatabase(cfg config.Config) *mysql.DB {
func initDatabase(cfg config.Config, migrate bool) *mysql.DB {
if migrate {
migrateDatabase(cfg)
}
return initial.InitMysql(cfg)
}