Skip to main content

Module layer

Module layer 

Source
Available on crate feature http only.
Expand description

The middleware every generated handler needs.

api!’s handlers take the request’s Context as an axum extension. Something has to put it there, and that is layer: it reads the client’s request id, generates one when there is none, and attaches the context.

ⓘ
let app = routes()
    .layer(ash_jsonapi::layer())
    .with_state(state);

Without it, every request fails with axum’s “Missing request extension” 500 — the handlers cannot build a context themselves, because what goes into one (the principal, the tenant) is decided by middleware that runs before them.

§Adding your own

Authentication runs after this and adds to what it built:

ⓘ
async fn authenticate(mut req: Request, next: Next) -> Response {
    let ctx = req
        .extensions()
        .get::<Context>()
        .expect("ash_jsonapi::layer() runs first")
        .clone()
        .with_principal("alice@example.com");

    req.extensions_mut().insert(ctx);
    next.run(req).await
}

Constants§

ACCEPT_LANGUAGE
The header the response locale is negotiated from.
REQUEST_ID
The header a correlation id is read from and echoed back on.

Functions§

layer
Attach a Context to every request.

Type Aliases§

WithContext
The future layer’s middleware returns.