package app import ( "fmt" "log" "net/http" "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/config" "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/controllers/taskcontroller" "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models" "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/router" "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/services/cronservice" "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/services/reposervice" "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/services/taskservice" "gorm.io/driver/postgres" "gorm.io/gorm" ) type App struct { db *gorm.DB config *config.Config repo *reposervice.Service taskService *taskservice.Service cron *cronservice.Service taskController *taskcontroller.TaskController router *router.Router } func createDBConnection(cfg *config.Config) (*gorm.DB, error) { dsn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", cfg.DBHost, cfg.DBPort, cfg.DBUser, cfg.DBPassword, cfg.DBName) return gorm.Open(postgres.Open(dsn), &gorm.Config{}) } func New() (*App, error) { cfg := config.New() db, err := createDBConnection(cfg) if err != nil { return nil, err } db.AutoMigrate(&models.Task{}, &models.GitRepoAltRepoTask{}, &models.RPMFile{}) db.FirstOrCreate(&models.ALTRepo{ Name: "Sisyphus", }) app := App{ config: cfg, db: db, } app.createServices() app.createControllers() app.createRouter() return &app, nil } func (app *App) createServices() { app.repo = reposervice.New( app.db, app.config, ) app.taskService = taskservice.New( app.db, app.config, ) app.cron = cronservice.New( app.repo, ) } func (app *App) createControllers() { app.taskController = taskcontroller.New( app.taskService, ) } func (app *App) createRouter() { app.router = router.New( app.config, app.taskController, ) } func (app *App) initServices() { app.cron.SetupCronJobs() app.cron.Start() } func (app *App) Init() { app.initServices() } func (app *App) Run() { log.Printf("Сервер запущен на порту: %s", app.config.Port) http.ListenAndServe(":"+app.config.Port, app.router.Setup()) }