- working with flags main func
This commit is contained in:
parent
bbe678fa46
commit
ee856cb8b0
74
cmd/main.go
74
cmd/main.go
|
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -22,18 +23,26 @@ import (
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const migrationfile = "../internal/storage/migrate"
|
||||||
envfile = ".env"
|
|
||||||
migrationfile = "../internal/storage/migrate"
|
|
||||||
// production env:
|
|
||||||
// envfile = "../docker/prod.env"
|
|
||||||
// migrationfile = "../internal/storage/migrate"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.Print("started")
|
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 {
|
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()
|
return group.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
func run(conf domain.Config) {
|
func applyMigrations(connString string) {
|
||||||
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:
|
// Aply migrations:
|
||||||
|
|
||||||
dbConnConfig, err := pgxpool.ParseConfig(connString)
|
dbConnConfig, err := pgxpool.ParseConfig(connString)
|
||||||
|
|
@ -87,26 +80,47 @@ func run(conf domain.Config) {
|
||||||
fmt.Printf("Applied %d migrations!\n", n)
|
fmt.Printf("Applied %d migrations!\n", n)
|
||||||
|
|
||||||
db.Close()
|
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)
|
gitService := services.NewGit(conf.Git)
|
||||||
cloudService := services.NewCloud(conf.Cloud)
|
cloudService := services.NewCloud(conf.Cloud)
|
||||||
codaService := services.NewCodaClient(conf.Coda)
|
codaService := services.NewCodaClient(conf.Coda)
|
||||||
|
|
||||||
// Инициализация контроллера:
|
// Controller instance init:
|
||||||
controller := controller.NewWorkflowController(
|
controller := controller.NewWorkflowController(
|
||||||
gitService,
|
gitService,
|
||||||
cloudService,
|
cloudService,
|
||||||
codaService,
|
codaService,
|
||||||
conn,
|
conn,
|
||||||
|
conf,
|
||||||
)
|
)
|
||||||
|
|
||||||
Go(ctx,
|
Go(ctx,
|
||||||
func(ctx context.Context) error {
|
func(ctx context.Context) error {
|
||||||
opts := discord.DiscordOptions{
|
opts := discord.DiscordOptions{
|
||||||
Controller: controller,
|
Controller: controller,
|
||||||
Config: &conf,
|
Config: conf,
|
||||||
}
|
}
|
||||||
if err := discord.Run(conf, opts); err != nil {
|
if err := discord.Run(conf, opts); err != nil {
|
||||||
return errors.Errorf("discord bot cannot be runned: %v", err)
|
return errors.Errorf("discord bot cannot be runned: %v", err)
|
||||||
|
|
@ -118,7 +132,7 @@ func run(conf domain.Config) {
|
||||||
GitService: gitService,
|
GitService: gitService,
|
||||||
CloudService: cloudService,
|
CloudService: cloudService,
|
||||||
Coda: codaService,
|
Coda: codaService,
|
||||||
AppConfig: &conf,
|
AppConfig: conf,
|
||||||
Controller: controller,
|
Controller: controller,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue