feat: Add REST API handlers for exercises, plans, and sessions
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
55
backend/internal/api/router.go
Normal file
55
backend/internal/api/router.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"training-tracker/internal/storage"
|
||||
)
|
||||
|
||||
// Router sets up and returns the HTTP router
|
||||
type Router struct {
|
||||
exerciseHandler *ExerciseHandler
|
||||
planHandler *PlanHandler
|
||||
sessionHandler *SessionHandler
|
||||
}
|
||||
|
||||
// NewRouter creates a new router with all handlers
|
||||
func NewRouter(db *storage.DB) *Router {
|
||||
exerciseRepo := storage.NewExerciseRepository(db)
|
||||
planRepo := storage.NewPlanRepository(db)
|
||||
sessionRepo := storage.NewSessionRepository(db)
|
||||
|
||||
return &Router{
|
||||
exerciseHandler: NewExerciseHandler(exerciseRepo),
|
||||
planHandler: NewPlanHandler(planRepo),
|
||||
sessionHandler: NewSessionHandler(sessionRepo),
|
||||
}
|
||||
}
|
||||
|
||||
// ServeHTTP implements http.Handler
|
||||
func (router *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
// Enable CORS
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
|
||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
||||
|
||||
if r.Method == http.MethodOptions {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
return
|
||||
}
|
||||
|
||||
path := r.URL.Path
|
||||
|
||||
switch {
|
||||
case strings.HasPrefix(path, "/api/exercises"):
|
||||
router.exerciseHandler.ServeHTTP(w, r)
|
||||
case strings.HasPrefix(path, "/api/plans"):
|
||||
router.planHandler.ServeHTTP(w, r)
|
||||
case strings.HasPrefix(path, "/api/sessions"):
|
||||
router.sessionHandler.ServeHTTP(w, r)
|
||||
case path == "/api/health":
|
||||
respondJSON(w, http.StatusOK, map[string]string{"status": "ok"})
|
||||
default:
|
||||
respondError(w, http.StatusNotFound, "not found")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user