Files
agentic-coding-workflow/internal/transport/http/router.go
T

51 lines
1.5 KiB
Go

package httpx
import (
"net/http"
"github.com/go-chi/chi/v5"
)
// RouterDeps bundles the dependencies needed to assemble the router.
// MVP exposes only liveness/readiness probe hooks; later phases (auth
// resolver, module handlers, etc.) will add fields here rather than
// growing NewRouter's signature.
type RouterDeps struct {
// HealthCheck is invoked from GET /healthz. Returning a non-nil
// error renders 503 with the error message as the body. If nil,
// /healthz unconditionally returns 200 "ok".
HealthCheck func(*http.Request) error
// ReadyCheck is invoked from GET /readyz with the same contract.
ReadyCheck func(*http.Request) error
}
// NewRouter builds the application's chi.Router with the standard
// liveness/readiness endpoints attached. The return type is the
// chi.Router interface (not *chi.Mux) so call sites depend only on
// behaviour shared with future routing layers.
func NewRouter(deps RouterDeps) chi.Router {
r := chi.NewRouter()
r.Get("/healthz", func(w http.ResponseWriter, req *http.Request) {
if deps.HealthCheck != nil {
if err := deps.HealthCheck(req); err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
}
_, _ = w.Write([]byte("ok"))
})
r.Get("/readyz", func(w http.ResponseWriter, req *http.Request) {
if deps.ReadyCheck != nil {
if err := deps.ReadyCheck(req); err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
}
_, _ = w.Write([]byte("ok"))
})
return r
}