aides-repo-api/internal/app/app.go

108 lines
2.2 KiB
Go
Raw Normal View History

package app
import (
"fmt"
2024-12-15 06:16:51 +00:00
"log"
"net/http"
2024-12-15 06:36:33 +00:00
"gorm.io/driver/postgres"
"gorm.io/gorm"
"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"
"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"
)
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) {
2024-12-15 06:36:33 +00:00
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,
)
2024-12-15 06:16:51 +00:00
return gorm.Open(postgres.Open(dsn), &gorm.Config{})
}
func New() (*App, error) {
cfg := config.New()
2024-12-15 06:16:51 +00:00
db, err := createDBConnection(cfg)
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())
}