61 lines
2.1 KiB
Go
61 lines
2.1 KiB
Go
// Package server composes HTTP and optional scheduled application execution.
|
|
// Concrete retrieval/provider adapters belong to the executable's composition root.
|
|
package server
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/tradarr/backend/internal/ai"
|
|
"github.com/tradarr/backend/internal/api"
|
|
"github.com/tradarr/backend/internal/api/handlers"
|
|
"github.com/tradarr/backend/internal/companycatalog"
|
|
"github.com/tradarr/backend/internal/config"
|
|
"github.com/tradarr/backend/internal/crypto"
|
|
"github.com/tradarr/backend/internal/generation"
|
|
"github.com/tradarr/backend/internal/indices"
|
|
"github.com/tradarr/backend/internal/models"
|
|
"github.com/tradarr/backend/internal/scheduler"
|
|
"github.com/tradarr/backend/internal/scraper"
|
|
"log"
|
|
)
|
|
|
|
type Dependencies struct {
|
|
CompanyCatalog companycatalog.CompanyCatalog
|
|
LayaRanker generation.LayaRanker
|
|
Feeds map[string]scraper.Scraper
|
|
IndexSearcher indices.IndexSearcher
|
|
GenerationFactory func(*ai.Pipeline) generation.UseCase
|
|
EnableScheduler bool
|
|
}
|
|
type App struct {
|
|
Router *gin.Engine
|
|
scheduler *scheduler.Scheduler
|
|
}
|
|
|
|
func New(repo *models.Repository, cfg *config.Config, deps Dependencies) *App {
|
|
enc := crypto.New(cfg.EncryptionKey)
|
|
pipeline := ai.NewPipeline(repo, enc)
|
|
if names, ok := deps.CompanyCatalog.(generation.MentionNameResolver); ok {
|
|
pipeline.SetMentionNameResolver(names)
|
|
}
|
|
registry := scraper.NewRegistry(repo)
|
|
uc := pipeline.NewGenerationUseCaseWithLaya(deps.Feeds, deps.LayaRanker)
|
|
if deps.GenerationFactory != nil {
|
|
uc = deps.GenerationFactory(pipeline)
|
|
}
|
|
sched := scheduler.New(registry, pipeline, repo)
|
|
sched.SetGenerationUseCase(uc)
|
|
if deps.EnableScheduler {
|
|
if err := sched.Start(); err != nil {
|
|
log.Printf("scheduler: %v", err)
|
|
}
|
|
}
|
|
h := handlers.New(repo, cfg, enc, registry, pipeline, sched)
|
|
h.SetGenerationUseCase(uc)
|
|
h.SetIndexSearcher(deps.IndexSearcher)
|
|
if deps.CompanyCatalog != nil {
|
|
h.SetCompanyCatalog(deps.CompanyCatalog)
|
|
}
|
|
return &App{Router: api.SetupRouter(h, cfg.JWTSecret), scheduler: sched}
|
|
}
|
|
func (a *App) Close() { a.scheduler.Stop() }
|