package server import ( "encoding/json" "net/http" "github.com/go-chi/chi/v5" ) // RouteHandler defines the interface for route handlers type RouteHandler interface { Health(w http.ResponseWriter, r *http.Request) GetCurrentWeather(w http.ResponseWriter, r *http.Request) GetForecast(w http.ResponseWriter, r *http.Request) GetHistorical(w http.ResponseWriter, r *http.Request) AssessConditions(w http.ResponseWriter, r *http.Request) } // SetupRoutes configures all API routes func (s *Server) SetupRoutes(handler RouteHandler) { // Health check endpoint s.router.Get("/api/health", handler.Health) // API v1 routes s.router.Route("/api/v1", func(r chi.Router) { // Weather routes r.Route("/weather", func(r chi.Router) { r.Get("/current", handler.GetCurrentWeather) r.Get("/forecast", handler.GetForecast) r.Get("/historical", handler.GetHistorical) r.Post("/assess", handler.AssessConditions) }) }) } // Response helpers // JSONResponse is a generic JSON response structure type JSONResponse struct { Success bool `json:"success"` Data interface{} `json:"data,omitempty"` Error string `json:"error,omitempty"` } // RespondJSON writes a JSON response func RespondJSON(w http.ResponseWriter, statusCode int, data interface{}) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(statusCode) response := JSONResponse{ Success: statusCode >= 200 && statusCode < 300, Data: data, } if err := json.NewEncoder(w).Encode(response); err != nil { http.Error(w, "Failed to encode response", http.StatusInternalServerError) } } // RespondError writes a JSON error response func RespondError(w http.ResponseWriter, statusCode int, message string) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(statusCode) response := JSONResponse{ Success: false, Error: message, } if err := json.NewEncoder(w).Encode(response); err != nil { http.Error(w, "Failed to encode error response", http.StatusInternalServerError) } }