Files
homelab-frontend/internal/server/router.go
T

44 lines
1.4 KiB
Go
Raw Normal View History

2026-08-19 20:52:13 -07:00
package server
import (
"net/http"
)
// Router implements an HTTP handler that routes health endpoints,
// Temporal workflow endpoints, and other requests to upstream handlers.
2026-08-19 20:52:13 -07:00
type Router struct {
healthChecker *HealthChecker
temporalHandler http.Handler
2026-08-19 20:52:13 -07:00
upstreamHandler http.Handler
}
// NewRouter creates a new router with health endpoints.
// Health endpoints (/healthz and /readyz) are handled locally.
// Temporal endpoints (/workflow*) are routed to temporalHandler.
2026-08-19 20:52:13 -07:00
// All other paths are passed to the upstream handler.
func NewRouter(healthChecker *HealthChecker, temporalHandler http.Handler, upstreamHandler http.Handler) *Router {
2026-08-19 20:52:13 -07:00
return &Router{
healthChecker: healthChecker,
temporalHandler: temporalHandler,
2026-08-19 20:52:13 -07:00
upstreamHandler: upstreamHandler,
}
}
// ServeHTTP implements http.Handler.
// It routes /healthz and /readyz to health handlers,
// /workflow* to the temporal handler,
2026-08-19 20:52:13 -07:00
// and passes all other paths to the upstream handler.
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
switch {
case req.URL.Path == "/healthz":
2026-08-19 20:52:13 -07:00
LivenessHandler(r.healthChecker)(w, req)
case req.URL.Path == "/readyz":
2026-08-19 20:52:13 -07:00
ReadinessHandler(r.healthChecker)(w, req)
case req.URL.Path == "/workflow" || req.URL.Path == "/workflow/health" || req.URL.Path == "/workflow/metrics":
// Route all /workflow endpoints to temporal handler
r.temporalHandler.ServeHTTP(w, req)
2026-08-19 20:52:13 -07:00
default:
r.upstreamHandler.ServeHTTP(w, req)
}
}