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

138 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 07:24:14 +00:00
"os"
2024-12-15 07:24:14 +00:00
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
2024-12-15 06:36:33 +00:00
"gorm.io/driver/postgres"
"gorm.io/gorm"
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"
"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 07:24:14 +00:00
logger *zap.Logger
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() {
atomic := zap.NewAtomicLevel()
atomic.SetLevel(zapcore.DebugLevel)
enconfig := zap.NewProductionEncoderConfig()
enconfig.EncodeTime = zapcore.ISO8601TimeEncoder
enconfig.TimeKey = "timestamp"
logger := zap.New(zapcore.NewCore(
zapcore.NewJSONEncoder(enconfig),
zapcore.Lock(os.Stdout),
atomic,
))
app.logger = logger
}
func (app *App) createDb() {
logger := zapgorm2.New(app.logger)
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)
}
db.AutoMigrate(&models.Task{}, &models.GitRepoAltRepoTask{}, &models.RPMFile{})
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(
"Сервер запущен",
zap.Int(
"port",
app.config.Port,
),
)
http.ListenAndServe(fmt.Sprintf(":%d", app.config.Port), app.router.Setup())
}