2026-08-19 20:52:13 -07:00
|
|
|
package server
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
)
|
|
|
|
|
|
2026-08-22 23:17:12 -07:00
|
|
|
// 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
|
2026-08-22 23:17:12 -07:00
|
|
|
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.
|
2026-08-22 23:17:12 -07:00
|
|
|
// Temporal endpoints (/workflow*) are routed to temporalHandler.
|
2026-08-19 20:52:13 -07:00
|
|
|
// All other paths are passed to the upstream handler.
|
2026-08-22 23:17:12 -07:00
|
|
|
func NewRouter(healthChecker *HealthChecker, temporalHandler http.Handler, upstreamHandler http.Handler) *Router {
|
2026-08-19 20:52:13 -07:00
|
|
|
return &Router{
|
|
|
|
|
healthChecker: healthChecker,
|
2026-08-22 23:17:12 -07:00
|
|
|
temporalHandler: temporalHandler,
|
2026-08-19 20:52:13 -07:00
|
|
|
upstreamHandler: upstreamHandler,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ServeHTTP implements http.Handler.
|
|
|
|
|
// It routes /healthz and /readyz to health handlers,
|
2026-08-22 23:17:12 -07:00
|
|
|
// /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) {
|
2026-08-22 23:17:12 -07:00
|
|
|
switch {
|
|
|
|
|
case req.URL.Path == "/healthz":
|
2026-08-19 20:52:13 -07:00
|
|
|
LivenessHandler(r.healthChecker)(w, req)
|
2026-08-22 23:17:12 -07:00
|
|
|
case req.URL.Path == "/readyz":
|
2026-08-19 20:52:13 -07:00
|
|
|
ReadinessHandler(r.healthChecker)(w, req)
|
2026-08-22 23:17:12 -07:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|