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

141 lines
2.7 KiB
Go
Raw Normal View History

package app
import (
"fmt"
2024-12-15 06:16:51 +00:00
"net/http"
2024-12-15 06:36:33 +00:00
"gorm.io/driver/postgres"
"gorm.io/gorm"
2024-12-15 09:03:11 +00:00
gormlogger "gorm.io/gorm/logger"
2024-12-15 07:24:14 +00:00
"moul.io/zapgorm2"
2024-12-15 06:36:33 +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-15 08:39:18 +00:00
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/logger"
"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 08:39:18 +00:00
logger *logger.ZapLogger
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 New() (*App, error) {
2024-12-15 07:24:14 +00:00
app := App{}
2024-12-15 07:24:14 +00:00
app.createLogger()
app.config = config.New()
2024-12-15 07:24:14 +00:00
app.createDb()
2024-12-15 06:16:51 +00:00
app.createServices()
app.createControllers()
app.createRouter()
return &app, nil
}
2024-12-15 07:24:14 +00:00
func (app *App) createLogger() {
2024-12-15 08:39:18 +00:00
app.logger = logger.GetLogger()
2024-12-15 07:24:14 +00:00
}
func (app *App) createDb() {
2024-12-15 08:39:18 +00:00
logger := zapgorm2.New(app.logger.GetZap())
2024-12-15 09:03:11 +00:00
logger.LogMode(gormlogger.Info)
2024-12-15 07:24:14 +00:00
logger.SetAsDefault()
dsn := fmt.Sprintf(
"host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
app.config.DBHost,
app.config.DBPort,
app.config.DBUser,
app.config.DBPassword,
app.config.DBName,
)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{Logger: logger})
if err != nil {
panic(err)
}
2024-12-15 08:39:18 +00:00
err = db.AutoMigrate(&models.Task{}, &models.GitRepoAltRepoTask{}, &models.RPMFile{})
if err != nil {
panic(err)
}
2024-12-15 07:24:14 +00:00
db.FirstOrCreate(&models.ALTRepo{
Name: "Sisyphus",
})
app.db = db
}
2024-12-15 06:16:51 +00:00
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() {
2024-12-15 07:24:14 +00:00
app.logger.Info(
"Сервер запущен",
2024-12-15 08:39:18 +00:00
map[string]interface{}{
"port": app.config.Port,
},
2024-12-15 07:24:14 +00:00
)
2025-01-03 08:04:12 +00:00
err := app.repo.Update(true)
2024-12-24 19:40:37 +00:00
if err != nil {
panic(err)
}
2024-12-16 16:34:51 +00:00
2024-12-24 19:40:37 +00:00
err = http.ListenAndServe(
fmt.Sprintf(":%d", app.config.Port),
app.router.Setup(),
)
2024-12-15 08:39:18 +00:00
if err != nil {
panic(err)
}
}
2024-12-24 19:40:37 +00:00
func (app *App) Shutdown() {
app.cron.Shutdown()
}