services WIP

This commit is contained in:
2025-10-18 00:45:57 +01:00
parent 1371fec036
commit 69b8c0cbe5
3 changed files with 689 additions and 47 deletions
+47 -47
View File
@@ -17,6 +17,7 @@ import (
authHandlers "crussell/handlers/auth"
"crussell/handlers/scheduling"
"crussell/handlers/services"
"crussell/handlers/user"
)
@@ -53,13 +54,12 @@ func main() {
r := chi.NewRouter()
// --- Middleware ---
r.Use(middleware.RequestID) // Add X-Request-ID header
r.Use(middleware.RealIP) // Get real IP from headers
r.Use(middleware.Logger) // Basic logging
r.Use(middleware.Recoverer) // Panic recovery
r.Use(middleware.Timeout(15 * time.Second)) // Request timeout
// --- Global Middleware ---
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(middleware.Timeout(15 * time.Second))
r.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Content-Type-Options", "nosniff")
@@ -69,55 +69,55 @@ func main() {
})
})
// --- Public auth routes ---
r.Post("/api/register", authHandlers.RegisterHandler)
r.Post("/api/login", authHandlers.LoginHandler)
// All API routes grouped under /api for clarity
r.Route("/api", func(r chi.Router) {
// --- Protected routes - any authenticated user ---
r.Group(func(r chi.Router) {
r.Use(mw.RequireAuth)
// --- Public Routes ---
r.Get("/services", services.ServicesHandler)
r.Post("/register", authHandlers.RegisterHandler)
r.Post("/login", authHandlers.LoginHandler)
// User profile
r.Get("/api/user/profile", user.GetProfileHandler)
r.Put("/api/user/profile", user.UpdateProfileHandler)
r.Delete("/api/user/account", user.DeleteAccountHandler)
// --- Scheduling public GET routes ---
r.Route("/scheduling", func(r chi.Router) {
r.Get("/default-hours", scheduling.GetDefaultHours)
r.Get("/exceptional-groups", scheduling.ListExceptionalGroups)
r.Get("/exceptional-applications", scheduling.ListExceptionalApplications)
r.Get("/working-hours", scheduling.GetWorkingHours)
r.Get("/available-hours", scheduling.GetAvailableHours)
// Loyalty
r.Get("/api/user/loyalty", user.GetLoyaltyHandler)
})
// Admin-only scheduling modifications
r.Group(func(r chi.Router) {
r.Use(mw.RequireAuth)
r.Use(mw.RequireAdmin)
// --- Scheduling routes ---
r.Route("/api/scheduling", func(r chi.Router) {
r.Put("/default-hours", scheduling.UpdateDefaultHours)
r.Post("/exceptional-groups", scheduling.CreateExceptionalGroup)
r.Post("/exceptional-applications", scheduling.CreateExceptionalApplication)
})
})
// Default hours
r.Get("/default-hours", scheduling.GetDefaultHours)
// --- Protected routes (any authenticated user) ---
r.Group(func(r chi.Router) {
r.Use(mw.RequireAuth)
r.Get("/user/profile", user.GetProfileHandler)
r.Put("/user/profile", user.UpdateProfileHandler)
r.Delete("/user/account", user.DeleteAccountHandler)
r.Get("/user/loyalty", user.GetLoyaltyHandler)
})
// --- Admin-only routes ---
r.Group(func(r chi.Router) {
r.Use(mw.RequireAuth)
r.Use(mw.RequireAdmin)
r.Put("/default-hours", scheduling.UpdateDefaultHours)
r.Route("/admin/services", func(r chi.Router) {
r.Post("/", services.CreateServiceHandler)
r.Delete("/{id}", services.DeleteServiceHandler)
r.Get("/", services.AllServicesHandler)
r.Put("/{id}/toggle", services.ToggleService)
})
})
// Exceptional groups
r.Get("/exceptional-groups", scheduling.ListExceptionalGroups)
r.Group(func(r chi.Router) {
r.Use(mw.RequireAuth)
r.Use(mw.RequireAdmin)
r.Post("/exceptional-groups", scheduling.CreateExceptionalGroup)
})
// Exceptional applications (assign groups to weeks)
r.Get("/exceptional-applications", scheduling.ListExceptionalApplications)
r.Group(func(r chi.Router) {
r.Use(mw.RequireAuth)
r.Use(mw.RequireAdmin)
r.Post("/exceptional-applications", scheduling.CreateExceptionalApplication)
})
// Merged working hours (default + exceptional)
r.Get("/working-hours", scheduling.GetWorkingHours)
// Fully calculated available hours (default + exceptional + bookings + lunch breaks)
r.Get("/available-hours", scheduling.GetAvailableHours)
})
fmt.Println("Server is listening on :8080")