diff --git a/cmd/main.go b/cmd/main.go index 94c641c..a9f5072 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "flag" "fmt" "log" "os" @@ -22,18 +23,26 @@ import ( "golang.org/x/sync/errgroup" ) -const ( - envfile = ".env" - migrationfile = "../internal/storage/migrate" - // production env: - // envfile = "../docker/prod.env" - // migrationfile = "../internal/storage/migrate" -) +const migrationfile = "../internal/storage/migrate" func main() { log.Print("started") - config := domain.InitConfig(envfile) - run(config) + + env := flag.Int("env", -1, "0 for development env file, 1 for production environment run, missing flag for build") + flag.Parse() + var envPath string + + switch *env { + case 0: + envPath = ".env" + case 1: + envPath = "../docker/prod.env" + default: + envPath = "prod.env" + } + + config := domain.InitConfig(envPath) + run(&config) } func Go(ctx context.Context, fns ...func(context.Context) error) error { @@ -49,23 +58,7 @@ func Go(ctx context.Context, fns ...func(context.Context) error) error { return group.Wait() } -func run(conf domain.Config) { - ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill, syscall.SIGTERM) - defer cancel() - - // -- DB connection init -- START - connString := fmt.Sprintf( - "postgresql://%s:%s@%s:%s/%s", - conf.DB.User, conf.DB.Pass, conf.DB.Host, conf.DB.Port, conf.DB.Name, - ) - conn, err := pgxpool.New( - ctx, - connString) - if err != nil { - log.Fatalf("DB connection failed: %v", err) - } - // -- DB connection init -- END - +func applyMigrations(connString string) { // Aply migrations: dbConnConfig, err := pgxpool.ParseConfig(connString) @@ -87,26 +80,47 @@ func run(conf domain.Config) { fmt.Printf("Applied %d migrations!\n", n) db.Close() +} - // +func run(conf *domain.Config) { + ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill, syscall.SIGTERM) + defer cancel() + // -- DB connection init -- START + connString := fmt.Sprintf( + "postgresql://%s:%s@%s:%s/%s", + conf.DB.User, conf.DB.Pass, conf.DB.Host, conf.DB.Port, conf.DB.Name, + ) + conn, err := pgxpool.New( + ctx, + connString) + if err != nil { + log.Fatalf("DB connection failed: %v", err) + } + // -- DB connection init -- END + + // Aply migrations: + applyMigrations(connString) + + // Init services instances: gitService := services.NewGit(conf.Git) cloudService := services.NewCloud(conf.Cloud) codaService := services.NewCodaClient(conf.Coda) - // Инициализация контроллера: + // Controller instance init: controller := controller.NewWorkflowController( gitService, cloudService, codaService, conn, + conf, ) Go(ctx, func(ctx context.Context) error { opts := discord.DiscordOptions{ Controller: controller, - Config: &conf, + Config: conf, } if err := discord.Run(conf, opts); err != nil { return errors.Errorf("discord bot cannot be runned: %v", err) @@ -118,7 +132,7 @@ func run(conf domain.Config) { GitService: gitService, CloudService: cloudService, Coda: codaService, - AppConfig: &conf, + AppConfig: conf, Controller: controller, }