downloader/api/internal/app/router/router.go

94 lines
3.0 KiB
Go

package router
import (
"downloader/internal/app/api/download"
"downloader/internal/app/infrastructure/logger"
"downloader/internal/app/persistence"
"downloader/internal/core/ports/download_request/sql"
"downloader/internal/core/ports/downloadhandler"
"downloader/internal/core/ports/filehandler/mover/local"
"downloader/internal/core/ports/fileorchestrator"
"downloader/internal/core/ports/fileorchestrator/destinationhandler"
"downloader/internal/core/ports/fileorchestrator/sourcehandler"
"downloader/internal/core/services/cleanup"
"downloader/internal/core/services/download/default"
"downloader/internal/core/services/download/handlers"
"downloader/internal/core/services/downloader"
"downloader/pkg/common/uuid"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/cors"
"net/http"
"time"
)
type router struct {
internalRouter *chi.Mux
}
func NewRouter() *router {
router := &router{internalRouter: chi.NewRouter()}
router.
setupMiddleware().
setupRoutes()
return router
}
func (router *router) Run() {
_ = http.ListenAndServe(":3333", router.internalRouter)
}
func (router *router) RegisterApi() *chi.Mux {
return router.internalRouter
}
func (router *router) setupMiddleware() *router {
router.internalRouter.Use(middleware.Logger)
router.internalRouter.Use(middleware.RequestID)
router.internalRouter.Use(middleware.RealIP)
router.internalRouter.Use(middleware.Recoverer)
router.internalRouter.Use(middleware.Timeout(time.Second * 60))
router.internalRouter.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{"https://*", "http://*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{"Link"},
AllowCredentials: false,
MaxAge: 300,
}))
return router
}
func (router *router) setupRoutes() *router {
setupDownloadRoute(router)
return router
}
func setupDownloadRoute(router *router) {
newLogger := logger.New()
sourceHandler := sourcehandler.New()
mover := local.New(newLogger)
destinationHandler := destinationhandler.New(mover)
fileOrchestrator := fileorchestrator.New(newLogger, sourceHandler, destinationHandler)
db := persistence.NewPostgresDB()
drRepository := sql.NewDownloadRequestSqlRepository(db, newLogger)
//drRepository := in_memory.NewInMemoryRepository(newLogger)
//dlHandler := downloadhandler.NewYoutubeDlDownloader(newLogger)
dlHandler := downloadhandler.NewYtDlpDownloader(newLogger)
ondlHandler := handlers.New(drRepository, newLogger)
dwnloader := downloader.New(newLogger, fileOrchestrator, dlHandler, ondlHandler)
drBackgroundService := _default.NewLocalBackgroundService(drRepository, newLogger, dwnloader)
gen := uuid.New()
drService := _default.NewLocalService(drRepository, gen, drBackgroundService, newLogger)
downloadApi := download.New(drService)
downloadApi.SetupDownloadApi(router.internalRouter)
cleanupJob := cleanup.New(drRepository, newLogger, fileOrchestrator)
cleanupJob.RunOnSchedule()
}