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

67 lines
1.4 KiB
Go
Raw Normal View History

2024-12-04 11:46:42 +00:00
package router
import (
2024-12-12 13:27:07 +00:00
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-co-op/gocron/v2"
2024-12-04 11:46:42 +00:00
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/app"
2024-12-12 13:27:07 +00:00
"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"
2024-12-12 13:27:07 +00:00
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/services/taskservice"
2024-12-04 11:46:42 +00:00
)
type Router struct {
app *app.App
2024-12-04 11:46:42 +00:00
}
func New(app *app.App) *Router {
2024-12-12 13:27:07 +00:00
return &Router{
app: app,
2024-12-12 13:27:07 +00:00
}
2024-12-04 11:46:42 +00:00
}
2024-12-12 13:27:07 +00:00
func (r *Router) Setup() *chi.Mux {
2024-12-04 11:46:42 +00:00
router := chi.NewRouter()
router.Use(middleware.Logger)
2024-12-12 13:27:07 +00:00
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()
},
),
2024-12-12 13:27:07 +00:00
)
2024-12-04 11:46:42 +00:00
2024-12-12 13:27:07 +00:00
taskController := taskcontroller.New(
r.app,
2024-12-12 13:27:07 +00:00
taskService,
)
2024-12-04 11:46:42 +00:00
authGuard := middlewares.CreateAuthGuard(r.app.Config)
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)
})
2024-12-12 13:27:07 +00:00
})
2024-12-04 11:46:42 +00:00
2024-12-12 13:27:07 +00:00
return router
2024-12-04 11:46:42 +00:00
}