package main import ( "crussell/auth" "crussell/internal/dav" "fmt" "log" "net/http" "os" "time" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" "crussell/db" "crussell/mw" authHandlers "crussell/handlers/auth" "crussell/handlers/scheduling" "crussell/handlers/services" "crussell/handlers/user" ) func init() { // 1. Read the environment variable jwtSecret := os.Getenv("JWT_SECRET_KEY") // 2. Add a check to ensure the secret is set if jwtSecret == "" { log.Fatal("FATAL: JWT_SECRET_KEY environment variable not set. Application cannot start.") } // 3. Use the environment variable for initialization auth.InitJWT(jwtSecret) } func initDB() { if err := db.Connect(); err != nil { log.Fatal("Failed to connect to DB:", err) } fmt.Println("Connected to DB successfully") } func initDav() { if dav.Service == nil { log.Fatal("Failed to initialize DAV service") } fmt.Println("DAV Service connected successfully") } func main() { initDB() initDav() r := chi.NewRouter() // --- 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") w.Header().Set("X-Frame-Options", "DENY") w.Header().Set("X-XSS-Protection", "1; mode=block") next.ServeHTTP(w, r) }) }) // All API routes grouped under /api for clarity r.Route("/api", func(r chi.Router) { // --- Public Routes --- r.Get("/services", services.ServicesHandler) r.Post("/register", authHandlers.RegisterHandler) r.Post("/login", authHandlers.LoginHandler) // --- 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) // Admin-only scheduling modifications r.Group(func(r chi.Router) { r.Use(mw.RequireAuth) r.Use(mw.RequireAdmin) r.Put("/default-hours", scheduling.UpdateDefaultHours) r.Post("/exceptional-groups", scheduling.CreateExceptionalGroup) r.Post("/exceptional-applications", scheduling.CreateExceptionalApplication) }) }) // --- 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.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) }) }) }) fmt.Println("Server is listening on :8080") http.ListenAndServe(":8080", r) }