37 lines
1019 B
Go
37 lines
1019 B
Go
package server
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"net/http"
|
||
|
|
)
|
||
|
|
|
||
|
|
// Router implements an HTTP handler that routes health endpoints
|
||
|
|
// and passes other requests to an upstream handler.
|
||
|
|
type Router struct {
|
||
|
|
healthChecker *HealthChecker
|
||
|
|
upstreamHandler http.Handler
|
||
|
|
}
|
||
|
|
|
||
|
|
// NewRouter creates a new router with health endpoints.
|
||
|
|
// Health endpoints (/healthz and /readyz) are handled locally.
|
||
|
|
// All other paths are passed to the upstream handler.
|
||
|
|
func NewRouter(healthChecker *HealthChecker, upstreamHandler http.Handler) *Router {
|
||
|
|
return &Router{
|
||
|
|
healthChecker: healthChecker,
|
||
|
|
upstreamHandler: upstreamHandler,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// ServeHTTP implements http.Handler.
|
||
|
|
// It routes /healthz and /readyz to health handlers,
|
||
|
|
// and passes all other paths to the upstream handler.
|
||
|
|
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||
|
|
switch req.URL.Path {
|
||
|
|
case "/healthz":
|
||
|
|
LivenessHandler(r.healthChecker)(w, req)
|
||
|
|
case "/readyz":
|
||
|
|
ReadinessHandler(r.healthChecker)(w, req)
|
||
|
|
default:
|
||
|
|
r.upstreamHandler.ServeHTTP(w, req)
|
||
|
|
}
|
||
|
|
}
|