mod middleware
srcGeneric middleware library for .
The Handler trait is already a perfectly good middleware
abstraction: a middleware is a handler that wraps another handler.
This module bundles the middlewares every flare app eventually
needs:
Logger[Inner]— log method / url / status / latency.RequestId[Inner]— attach an opaque request id (read on inbound, echo on outbound).Compress[Inner]— content-encoding negotiation (Accept-Encodingq-values per RFC 9110 paragraph 12.5.3), encoding the response body in gzip or brotli.CatchPanic[Inner]— turn anyraisefrom the inner handler into a sanitised 500 response so the connection is not torn down (theHttpServeralready does this;CatchPanicis for inner-only middleware stacks where the server isn't reached).
Each middleware is generic over its inner Handler so the chain
stays monomorphised — no virtual dispatch, no allocation per
request.
Functions
| fn negotiate_encoding | Pick the best ``Content-Encoding`` for an ``Accept-Encoding``. |
Structs
| struct Logger | Log method, url, status, and latency around the inner handler. |
| struct RequestId | Echo the inbound ``X-Request-Id`` header back on the response. |
| struct Compress | Negotiate ``Content-Encoding`` per RFC 9110 paragraph 12.5.3. |
| struct CatchPanic | Convert any ``raise`` from the inner handler into a 500. |
Functions
fn negotiate_encoding §
Pick the best ``Content-Encoding`` for an ``Accept-Encoding``.
Walks every comma-separated entry, parses ;q=<weight> per
RFC 9110 paragraph 12.5.3, and returns the highest-q entry from
{br, gzip, identity} that the client accepts. Ties
break on brotli > gzip > identity (matches nginx default).
Args
| accept | String |
Raw |
| brotli_ok | Bool |
Whether brotli is linkable on this build. |
Returns
| _AcceptEncodingPick |
|
Structs
struct Logger §
struct Logger[Inner: Handler & Copyable & Defaultable]
Log method, url, status, and latency around the inner handler.
Output goes to stdout via print. The format is intentionally
machine-grep-friendly so you can pipe it through jq /
awk without a structured-logging dep.
Fields
| inner | Inner | The wrapped handler. |
| prefix | String | Prefix prepended to every log line; defaults to ``"[flare]"``. |
Methods
struct RequestId §
struct RequestId[Inner: Handler & Copyable & Defaultable]
Echo the inbound ``X-Request-Id`` header back on the response.
If absent on the inbound side, a deterministic id derived from
perf_counter_ns is generated. Useful for request tracing
when paired with the upstream gateway / load balancer.
Fields
| inner | Inner |
Methods
fn __init__ static §
__init__(out self, var inner: Inner)
Args
| inner var | Inner | |
| self out | Self |
Returns
| Self |
struct Compress §
struct Compress[Inner: Handler & Copyable & Defaultable]
Negotiate ``Content-Encoding`` per RFC 9110 paragraph 12.5.3.
Inspects the inbound Accept-Encoding header, picks the
highest-q entry from {br, gzip, identity}, and
encodes the inner response body accordingly. Sets
Content-Encoding and Vary: Accept-Encoding on the
response.
Bodies smaller than min_size_bytes (default 1024) are passed
through untouched — the per-request encoder overhead beats the
transfer-time savings on small bodies.
Methods
struct CatchPanic §
struct CatchPanic[Inner: Handler & Copyable & Defaultable]
Convert any ``raise`` from the inner handler into a 500.
Useful when stacking middleware below the server's own
error-sanitisation layer (e.g. inside a router branch). The
server's HttpServer.serve path already wraps the top-level
handler this way; this is for inner stacks.
Fields
| inner | Inner | |
| body | String |