Add JSON validation middleware to ensure valid API responses

- Created JSONValidationMiddleware that validates all JSON responses
- Ensures that even on timeout or internal errors, a valid JSON error response is returned
- Applied to all API endpoints including /status.json
- Prevents client-side JSON parse errors when server encounters issues
This commit is contained in:
2025-07-29 04:13:01 +02:00
parent 7aec01c499
commit 3338e92785
4 changed files with 1840812 additions and 1487242 deletions

View File

@@ -23,7 +23,7 @@ func (s *Server) setupRoutes() {
// Routes
r.Get("/", s.handleRoot())
r.Get("/status", s.handleStatusHTML())
r.Get("/status.json", s.handleStatusJSON())
r.Get("/status.json", JSONValidationMiddleware(s.handleStatusJSON()).ServeHTTP)
// AS and prefix detail pages
r.Get("/as/{asn}", s.handleASDetail())
@@ -33,6 +33,7 @@ func (s *Server) setupRoutes() {
// API routes
r.Route("/api/v1", func(r chi.Router) {
r.Use(JSONValidationMiddleware)
r.Get("/stats", s.handleStats())
r.Get("/ip/{ip}", s.handleIPLookup())
r.Get("/as/{asn}", s.handleASDetailJSON())