pub trait Middleware: Send + Sync {
// Required method
fn handle<'life0, 'async_trait>(
&'life0 self,
request: Request,
next: Next,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
}Expand description
Trait for implementing middleware
Middleware can inspect/modify requests, short-circuit responses, or pass control
to the next middleware in the chain by calling next(request).await.
§Example
ⓘ
use kit::{async_trait, Middleware, Next, Request, Response, HttpResponse};
pub struct LoggingMiddleware;
#[async_trait]
impl Middleware for LoggingMiddleware {
async fn handle(&self, request: Request, next: Next) -> Response {
println!("--> {} {}", request.method(), request.path());
let response = next(request).await;
println!("<-- complete");
response
}
}Required Methods§
Sourcefn handle<'life0, 'async_trait>(
&'life0 self,
request: Request,
next: Next,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn handle<'life0, 'async_trait>(
&'life0 self,
request: Request,
next: Next,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Handle the request
- Call
next(request).awaitto pass control to the next middleware - Return
Err(HttpResponse)to short-circuit and respond immediately - Modify the response after calling
next()for post-processing