2024-12-14 08:35:45 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-12-15 06:16:51 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2024-12-14 08:35:45 +00:00
|
|
|
|
|
|
|
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/config"
|
2024-12-15 06:16:51 +00:00
|
|
|
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/controllers/taskcontroller"
|
2024-12-14 08:35:45 +00:00
|
|
|
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models"
|
2024-12-15 06:16:51 +00:00
|
|
|
"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"
|
2024-12-14 08:35:45 +00:00
|
|
|
"gorm.io/driver/postgres"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type App struct {
|
2024-12-15 06:16:51 +00:00
|
|
|
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{})
|
2024-12-14 08:35:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func New() (*App, error) {
|
|
|
|
cfg := config.New()
|
2024-12-15 06:16:51 +00:00
|
|
|
db, err := createDBConnection(cfg)
|
2024-12-14 08:35:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
db.AutoMigrate(&models.Task{}, &models.GitRepoAltRepoTask{}, &models.RPMFile{})
|
|
|
|
db.FirstOrCreate(&models.ALTRepo{
|
|
|
|
Name: "Sisyphus",
|
|
|
|
})
|
|
|
|
|
2024-12-15 06:16:51 +00:00
|
|
|
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())
|
2024-12-14 08:35:45 +00:00
|
|
|
}
|