Skip to content

Middleware and endpoints ​

PHP declares route behaviour with attributes. In Rust, each one is route middleware, or for the OpenAPI document, an openapi::Endpoint declared in Config.

PHPRust
#[RequiresScope]RequiresScope middleware
#[CacheControl]CacheControl middleware
#[CacheResponse]CacheResponse middleware
#[Endpoint]Config::endpoint
#[Describe]Resource::describe and ApiQuery::describe
#[Description]ErrorCode::description
The fusion-stack middleware groupThe FusionStack value, as middleware on your API routes

FusionStack ​

FusionStack is both a service provider and middleware. Register it as a provider, which renders errors, registers the migrations and the console commands, and add it as middleware to the API's routes, which adds request IDs, authentication, rate limits, versioning, idempotency, HTTP caching and usage tracking:

rust
route.prefix("/v1").middleware(api).group(|route| {
    route.get("/orders", orders_index);
});

RequiresScope ​

Guests get a 401 unauthenticated, and clients without the scope a 403 insufficient_scope. describe says what the scope allows, for the docs.

rust
route
    .post("/orders/{order}/cancel", orders_cancel)
    .middleware(RequiresScope::new("orders:write").describe("Change orders, such as cancelling them."));

CacheControl ​

A route's own Cache-Control for successful GET responses. Directives are sorted, and private is added unless public, private or s-maxage is set, as Symfony does. Other responses get no-cache, private.

rust
route.get("/customers", customers_index).middleware(CacheControl::new("private, max-age=10"));

CacheResponse ​

Keeps successful GET responses in the cache for ttl seconds, per client, version and query. Responses carry X-Response-Cache: hit or miss. tags may name route parameters, and shared() shares one entry between clients.

rust
route
    .get("/orders/{order}", orders_show)
    .middleware(CacheResponse::new(60).tags(["orders:{order}", "customers"]));

Flush tagged entries after a write with response_cache::flush(["orders", "orders:5"]), or flush_in(store, tags) for another store.

Endpoint ​

What the OpenAPI document says about an endpoint, keyed by method and URI:

rust
use fusion_stack::openapi::Endpoint;

Config::new().endpoint(
    "GET /v1/orders",
    Endpoint::new("List orders")
        .description("Orders, newest first.")
        .query(&orders())
        .collection(),
)
MethodWhat it does
Endpoint::new(summary) / unnamed()With a summary, or without one (the document uses the method and path).
description(text)A longer description, in Markdown.
operation_id(id)The operation's ID, in place of one made from the method and path.
tags([...])Tags in place of the path's first segment.
status(code)The success status, when it is not 200, or 201 for a POST that creates.
resource(kind) / collection()Responds with one resource, or a page of them.
query(&ApiQuery)Its include, fields, filter, sort and page parameters.
json_api_body(schema, example) / body(schema, example)The request body.
scope(scope, text)A scope it requires, as its RequiresScope does.
hidden()Leaves it out of the document.

Only declared endpoints are in the document; openapi::undeclared(app.routes()) lists FusionStack routes without one.

Released under the MIT License.