Module saphir::middleware[][src]

A middleware is an object being called before the request is processed by the router, allowing to continue or stop the processing of a given request by calling / omitting next.

       chain.next(_)       chain.next(_)
             |               |
             |               |
+---------+  |  +---------+  |  +---------+
|         +--+->+         +--+->+         |
| Middle  |     | Middle  |     |  Router |
| ware1   |     | ware2   |     |         |
|         +<----+         +<----+         |
+---------+     +---------+     +---------+

Once the request is fully processed by the stack or whenever a middleware returns an error, the request is terminated and the response is generated, the response then becomes available to the middleware

A middleware is defined as the following:

async fn example_middleware(data: &CustomData, ctx: HttpContext, chain: &dyn MiddlewareChain) -> Result<HttpContext, SaphirError> {
    // Do work before the request is handled by the router

    let ctx = chain.next(ctx).await?;

    // Do work with the response

    Ok(ctx)
}

SAFETY NOTICE

Inside the middleware chain we need a little bit of unsafe code. This code allow us to consider the futures generated by the middlewares as ’static. This is considered safe since all middleware data lives within the server stack which has a static lifetime over your application. We plan to remove this unsafe code as soon as we find another solution to it.

Structs

Builder

Builder to apply middleware onto the http stack

Traits

Middleware