Documentation
¶
Overview ¶
Package cellmw provides reusable RouteHandler wrappers for use in cell slice handlers.
Why this lives in runtime/, not cells/ ¶
Cell slice handlers that wrap a kcell.RouteHandler to inject middleware must forward DeclareHTTPContract(contractspec.ContractSpec) to satisfy the kcell.HTTPContractDeclarer interface that auth.Mount type-asserts at route registration time. Naming contractspec.ContractSpec in a cells/ file is forbidden by archtest CELLS-NO-CONTRACTSPEC-IMPORT-01 (contractspec is for generated contracts and runtime/ framework infra only). Moving the wrapper here — where runtime/ may freely import kernel/contractspec — resolves the violation while keeping the wrapper reusable across all cell slices.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WrapBeforeCommit ¶
func WrapBeforeCommit(w http.ResponseWriter, hook func(status int)) http.ResponseWriter
WrapBeforeCommit wraps w so that hook(status) runs exactly once, immediately before the response status is committed — at the explicit WriteHeader call, or at the implicit WriteHeader(200) on the first Write. hook is the caller's chance to set response headers (e.g. Set-Cookie) on w before they are flushed.
The wrapper is built with httpsnoop.Wrap, which preserves the underlying writer's optional interfaces (Flusher / Hijacker / ReaderFrom / Pusher) — matching the runtime/http middleware convention (recorder.go, buffer_writer.go). This is the sole reason the wrapper lives here in runtime/ rather than inline in a cells/ slice: cells/ may not import httpsnoop (cells-isolation depguard), so the response-writer plumbing lives in runtime/ while the cell supplies only the header-setting hook.
Types ¶
type HeaderInjectMux ¶
type HeaderInjectMux struct {
// contains filtered or unexported fields
}
HeaderInjectMux wraps a kcell.RouteHandler and applies a middleware to every handler registered via Handle, while transparently forwarding Prefix / DeclareAuthMeta / DeclareHTTPContract to the inner mux so the framework's type-assertions still see the inner mux's capabilities.
Typical use: a slice's RegisterRoutes wraps the incoming mux with NewHeaderInjectMux to inject a request-header value into ctx before the generated handler runs, without needing to name contractspec.ContractSpec in the slice package.
func NewHeaderInjectMux ¶
func NewHeaderInjectMux(inner kcell.RouteHandler, wrap func(http.Handler) http.Handler) *HeaderInjectMux
NewHeaderInjectMux returns a HeaderInjectMux that wraps inner with wrap on every Handle call. Both inner and wrap must be non-nil.
func (*HeaderInjectMux) DeclareAuthMeta ¶
func (m *HeaderInjectMux) DeclareAuthMeta(meta kcell.AuthRouteMeta) error
DeclareAuthMeta forwards auth-route metadata to the inner mux. auth.Mount type-asserts to kcell.AuthRouteDeclarer; without this forwarder the wrapper swallows the assertion and the router's policy-coverage check flags the route as "registered without auth.Mount".
func (*HeaderInjectMux) DeclareHTTPContract ¶
func (m *HeaderInjectMux) DeclareHTTPContract(spec contractspec.ContractSpec) error
DeclareHTTPContract forwards the route's ContractSpec to the inner mux. auth.Mount type-asserts to kcell.HTTPContractDeclarer; without this forwarder the wrapper swallows the assertion and the router loses the contract binding required for observability and governance.
This method is the sole reason this package lives in runtime/ rather than being inline in a cells/ slice: naming contractspec.ContractSpec in cells/ is forbidden by CELLS-NO-CONTRACTSPEC-IMPORT-01.
func (*HeaderInjectMux) Handle ¶
func (m *HeaderInjectMux) Handle(pattern string, h http.Handler)
Handle wraps h with the injected middleware and registers the result on the inner mux.
func (*HeaderInjectMux) Prefix ¶
func (m *HeaderInjectMux) Prefix() string
Prefix delegates to the inner mux if it implements kcell.Prefixer. auth.Mount type-asserts the mux to Prefixer to compute the chi-relative registration path; without this forwarder the wrapper would swallow the assertion and the route would be registered with an incorrect path.