Files
Datarush/internal/gw/router/router.go
T
2025-12-17 11:00:50 +03:00

114 lines
4.8 KiB
Go

package router
import (
"net/http"
"datarush/internal/gw/handler"
"datarush/internal/gw/middleware"
"github.com/gorilla/mux"
)
type Router struct {
authHandler *handler.AuthHandler
competitionHandler *handler.CompetitionHandler
taskHandler *handler.TaskHandler
submissionHandler *handler.SubmissionHandler
resultsHandler *handler.ResultsHandler
reviewHandler *handler.ReviewHandler
achievementsHandler *handler.AchievementsHandler
pingHandler *handler.PingHandler
authMiddleware *middleware.AuthMiddleware
}
func NewRouter(
authHandler *handler.AuthHandler,
competitionHandler *handler.CompetitionHandler,
taskHandler *handler.TaskHandler,
submissionHandler *handler.SubmissionHandler,
resultsHandler *handler.ResultsHandler,
reviewHandler *handler.ReviewHandler,
achievementsHandler *handler.AchievementsHandler,
pingHandler *handler.PingHandler,
authMiddleware *middleware.AuthMiddleware,
) *Router {
return &Router{
authHandler: authHandler,
competitionHandler: competitionHandler,
taskHandler: taskHandler,
submissionHandler: submissionHandler,
resultsHandler: resultsHandler,
reviewHandler: reviewHandler,
achievementsHandler: achievementsHandler,
pingHandler: pingHandler,
authMiddleware: authMiddleware,
}
}
func (rt *Router) Setup() http.Handler {
r := mux.NewRouter()
r.Use(middleware.LoggingMiddleware)
r.Use(middleware.CORSMiddleware)
api := r.PathPrefix("/api/v1").Subrouter()
api.HandleFunc("/ping", rt.pingHandler.Ping).Methods(http.MethodGet)
api.HandleFunc("/sign-up", rt.authHandler.SignUp).Methods(http.MethodPost)
api.HandleFunc("/sign-in", rt.authHandler.SignIn).Methods(http.MethodPost)
protected := api.PathPrefix("").Subrouter()
protected.Use(rt.authMiddleware.Authenticate)
protected.HandleFunc("/me", rt.authHandler.GetMe).Methods(http.MethodGet)
protected.HandleFunc("/competitions", rt.competitionHandler.CreateCompetition).Methods(http.MethodPost)
protected.HandleFunc("/competitions", rt.competitionHandler.ListCompetitions).Methods(http.MethodGet)
protected.HandleFunc("/competitions/{competition_id}", rt.competitionHandler.GetCompetition).Methods(http.MethodGet)
protected.HandleFunc("/competitions/{competition_id}", rt.competitionHandler.UpdateCompetition).
Methods(http.MethodPut)
protected.HandleFunc("/competitions/{competition_id}", rt.competitionHandler.DeleteCompetition).
Methods(http.MethodDelete)
protected.HandleFunc("/competitions/{competition_id}/state", rt.competitionHandler.ChangeCompetitionState).
Methods(http.MethodPatch)
protected.HandleFunc("/competitions/{competition_id}/join", rt.competitionHandler.JoinCompetition).
Methods(http.MethodPost)
protected.HandleFunc("/competitions/{competition_id}/tasks", rt.taskHandler.CreateTask).Methods(http.MethodPost)
protected.HandleFunc("/competitions/{competition_id}/tasks", rt.taskHandler.ListTasks).Methods(http.MethodGet)
protected.HandleFunc("/competitions/{competition_id}/tasks/{task_id}", rt.taskHandler.GetTask).
Methods(http.MethodGet)
protected.HandleFunc("/competitions/{competition_id}/tasks/{task_id}", rt.taskHandler.UpdateTask).
Methods(http.MethodPut)
protected.HandleFunc("/competitions/{competition_id}/tasks/{task_id}", rt.taskHandler.DeleteTask).
Methods(http.MethodDelete)
protected.HandleFunc("/competitions/{competition_id}/tasks/{task_id}/submit", rt.submissionHandler.SubmitTask).
Methods(http.MethodPost)
protected.HandleFunc("/competitions/{competition_id}/tasks/{task_id}/history", rt.submissionHandler.GetSubmissionHistory).
Methods(http.MethodGet)
protected.HandleFunc("/competitions/{competition_id}/results", rt.resultsHandler.GetCompetitionResults).
Methods(http.MethodGet)
protected.HandleFunc("/competitions/{competition_id}/results/me", rt.resultsHandler.GetMyResults).
Methods(http.MethodGet)
protected.HandleFunc("/competitions/{competition_id}/results/recalculate", rt.resultsHandler.RecalculateResults).
Methods(http.MethodPost)
protected.HandleFunc("/achievements", rt.achievementsHandler.ListAchievements).Methods(http.MethodGet)
protected.HandleFunc("/achievements/{achievement_id}", rt.achievementsHandler.GetAchievement).
Methods(http.MethodGet)
protected.HandleFunc("/users/{user_id}/achievements", rt.achievementsHandler.GetUserAchievements).
Methods(http.MethodGet)
api.HandleFunc("/review/{token}/submissions", rt.reviewHandler.ListSubmissionsForReview).Methods(http.MethodGet)
api.HandleFunc("/review/{token}/submissions/{submission_id}", rt.reviewHandler.GetSubmissionForReview).
Methods(http.MethodGet)
api.HandleFunc("/review/{token}/submissions/{submission_id}/evaluate", rt.reviewHandler.EvaluateSubmission).
Methods(http.MethodPost)
api.HandleFunc("/review/{token}/submissions/{submission_id}/release", rt.reviewHandler.ReleaseSubmission).
Methods(http.MethodPost)
return r
}