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

37 lines
756 B
Go
Raw Normal View History

2024-12-12 20:36:38 +00:00
package app
import (
2024-12-13 20:39:41 +00:00
"fmt"
2024-12-12 20:36:38 +00:00
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/config"
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models"
2024-12-13 20:39:41 +00:00
"gorm.io/driver/postgres"
2024-12-12 20:36:38 +00:00
"gorm.io/gorm"
)
type App struct {
Db *gorm.DB
Config *config.Config
}
func New() (*App, error) {
2024-12-13 20:39:41 +00:00
cfg := config.New()
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)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
2024-12-12 20:36:38 +00:00
if err != nil {
return nil, err
}
2024-12-13 10:52:15 +00:00
db.AutoMigrate(&models.Task{}, &models.GitRepoAltRepoTask{}, &models.RPMFile{})
2024-12-12 20:36:38 +00:00
db.FirstOrCreate(&models.ALTRepo{
Name: "Sisyphus",
})
return &App{
2024-12-13 20:39:41 +00:00
Config: cfg,
2024-12-12 20:36:38 +00:00
Db: db,
}, nil
}