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

94 lines
3.0 KiB
Go
Raw Normal View History

2021-12-21 00:17:09 +01:00
package router
import (
"downloader/internal/app/api/download"
2021-12-22 01:22:33 +01:00
"downloader/internal/app/infrastructure/logger"
2021-12-22 20:21:24 +01:00
"downloader/internal/app/persistence"
"downloader/internal/core/ports/download_request/sql"
2021-12-22 01:22:33 +01:00
"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"
2021-12-24 17:43:07 +01:00
"downloader/internal/core/services/cleanup"
2021-12-21 23:05:00 +01:00
"downloader/internal/core/services/download/default"
2021-12-22 01:22:33 +01:00
"downloader/internal/core/services/download/handlers"
"downloader/internal/core/services/downloader"
2021-12-21 02:18:11 +01:00
"downloader/pkg/common/uuid"
2021-12-21 00:17:09 +01:00
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
2021-12-26 00:02:51 +01:00
"github.com/go-chi/cors"
2021-12-21 00:17:09 +01:00
"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() {
2021-12-21 02:18:11 +01:00
_ = http.ListenAndServe(":3333", router.internalRouter)
2021-12-21 00:17:09 +01:00
}
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))
2021-12-26 00:02:51 +01:00
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,
}))
2021-12-21 00:17:09 +01:00
return router
}
func (router *router) setupRoutes() *router {
2021-12-21 02:18:11 +01:00
setupDownloadRoute(router)
2021-12-21 00:17:09 +01:00
return router
}
2021-12-21 02:18:11 +01:00
func setupDownloadRoute(router *router) {
2021-12-22 01:22:33 +01:00
newLogger := logger.New()
sourceHandler := sourcehandler.New()
mover := local.New(newLogger)
destinationHandler := destinationhandler.New(mover)
fileOrchestrator := fileorchestrator.New(newLogger, sourceHandler, destinationHandler)
2021-12-22 20:21:24 +01:00
db := persistence.NewPostgresDB()
drRepository := sql.NewDownloadRequestSqlRepository(db, newLogger)
//drRepository := in_memory.NewInMemoryRepository(newLogger)
2021-12-22 01:22:33 +01:00
//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)
2021-12-21 02:18:11 +01:00
gen := uuid.New()
2021-12-22 01:22:33 +01:00
drService := _default.NewLocalService(drRepository, gen, drBackgroundService, newLogger)
2021-12-21 02:18:11 +01:00
downloadApi := download.New(drService)
downloadApi.SetupDownloadApi(router.internalRouter)
2021-12-24 17:43:07 +01:00
2021-12-26 00:02:51 +01:00
cleanupJob := cleanup.New(drRepository, newLogger, fileOrchestrator)
2021-12-24 17:43:07 +01:00
cleanupJob.RunOnSchedule()
2021-12-21 02:18:11 +01:00
}