check / check (push) Successful in 1m13s
The CORS wildcard was global, so it also covered the Basic-Auth protected /metrics, which REPO_POLICIES.md forbids, and it allowed POST, PUT and DELETE, which no route serves, plus the Authorization and X-CSRF-Token headers. CORS now sits on a router holding only the public routes and allows GET and OPTIONS with the Accept and Content-Type headers. /metrics gets no CORS at all. Both are mounted routers rather than a Group: chi answers OPTIONS on a Group's route with 405 before its middleware runs, and any method /metrics does not register would otherwise fall through to the public router. So every method on /metrics now meets Basic Auth first, and /metrics/ is served like /metrics. Model: opus-5-5
75 lines
2.0 KiB
Go
75 lines
2.0 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
chimw "github.com/go-chi/chi/v5/middleware"
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
|
|
"sneak.berlin/go/dnswatcher/static"
|
|
)
|
|
|
|
// requestTimeout is the maximum duration for handling a request.
|
|
const requestTimeout = 60 * time.Second
|
|
|
|
// SetupRoutes configures all HTTP routes.
|
|
func (s *Server) SetupRoutes() {
|
|
s.router = chi.NewRouter()
|
|
|
|
// Global middleware
|
|
s.router.Use(chimw.Recoverer)
|
|
s.router.Use(chimw.RequestID)
|
|
s.router.Use(s.mw.SecurityHeaders())
|
|
s.router.Use(s.mw.Logging())
|
|
s.router.Use(chimw.Timeout(requestTimeout))
|
|
|
|
// Public, unauthenticated, read-only routes, the only ones
|
|
// REPO_POLICIES.md allows wildcard CORS on. CORS is middleware of
|
|
// this whole router, not of a Group, so that it also answers
|
|
// OPTIONS preflight requests, which no route here registers.
|
|
public := chi.NewRouter()
|
|
public.Use(s.mw.CORS())
|
|
|
|
// Dashboard (read-only web UI)
|
|
public.Get("/", s.handlers.HandleDashboard())
|
|
|
|
// Static assets (embedded CSS/JS)
|
|
public.Mount(
|
|
"/s",
|
|
http.StripPrefix(
|
|
"/s",
|
|
http.FileServer(http.FS(static.Static)),
|
|
),
|
|
)
|
|
|
|
// Health check (standard well-known path)
|
|
public.Get(
|
|
"/.well-known/healthcheck",
|
|
s.handlers.HandleHealthCheck(),
|
|
)
|
|
|
|
// Legacy health check (keep for backward compatibility)
|
|
public.Get("/health", s.handlers.HandleHealthCheck())
|
|
|
|
// API v1 routes
|
|
public.Route("/api/v1", func(r chi.Router) {
|
|
r.Get("/status", s.handlers.HandleStatus())
|
|
})
|
|
|
|
s.router.Mount("/", public)
|
|
|
|
// Metrics endpoint (optional, with basic auth) and no CORS: a
|
|
// Prometheus scraper is not a browser. It is mounted rather than
|
|
// added with Get so that every method on /metrics, OPTIONS
|
|
// included, ends here instead of falling through to the public
|
|
// router and its CORS.
|
|
if s.params.Config.MetricsUsername != "" {
|
|
metrics := chi.NewRouter()
|
|
metrics.Use(s.mw.MetricsAuth())
|
|
metrics.Get("/", promhttp.Handler().ServeHTTP)
|
|
s.router.Mount("/metrics", metrics)
|
|
}
|
|
}
|