71 lines
1.5 KiB
Go
71 lines
1.5 KiB
Go
package router
|
|
|
|
import (
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/go-chi/chi/v5/middleware"
|
|
"github.com/go-co-op/gocron/v2"
|
|
httpSwagger "github.com/swaggo/http-swagger"
|
|
|
|
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/app"
|
|
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/controllers/taskcontroller"
|
|
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/middlewares"
|
|
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/services/reposervice"
|
|
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/services/taskservice"
|
|
|
|
_ "code.alt-gnome.ru/aides-infra/aides-repo-api/docs"
|
|
)
|
|
|
|
type Router struct {
|
|
app *app.App
|
|
}
|
|
|
|
func New(app *app.App) *Router {
|
|
return &Router{
|
|
app: app,
|
|
}
|
|
}
|
|
|
|
func (r *Router) Setup() *chi.Mux {
|
|
router := chi.NewRouter()
|
|
router.Use(middleware.Logger)
|
|
|
|
taskService := taskservice.New(
|
|
r.app,
|
|
)
|
|
|
|
repoService := reposervice.New(r.app)
|
|
repoService.ForceUpdate()
|
|
|
|
s, _ := gocron.NewScheduler()
|
|
defer func() { _ = s.Shutdown() }()
|
|
|
|
_, _ = s.NewJob(
|
|
gocron.CronJob(
|
|
"0 4 * * *",
|
|
false,
|
|
),
|
|
gocron.NewTask(
|
|
func() {
|
|
repoService.ForceUpdate()
|
|
},
|
|
),
|
|
)
|
|
|
|
taskController := taskcontroller.New(
|
|
r.app,
|
|
taskService,
|
|
)
|
|
|
|
authGuard := middlewares.CreateAuthGuard(r.app.Config)
|
|
|
|
router.Get("/swagger/*", httpSwagger.WrapHandler)
|
|
|
|
router.Route("/tasks", func(taskRouter chi.Router) {
|
|
taskRouter.With(authGuard).Post("/", taskController.Create)
|
|
taskRouter.Route("/{taskID}", func(sTaskRouter chi.Router) {
|
|
sTaskRouter.With(authGuard).Post("/upload", taskController.Upload)
|
|
})
|
|
})
|
|
|
|
return router
|
|
}
|