pub trait NgynMiddleware: Send + Sync {
// Required method
fn handle(cx: &mut NgynContext<'_>) -> impl Future<Output = ()> + Send
where Self: Sized;
}
Expand description
Trait for implementing a middleware.
Middlewares are how Ngyn processes requests. They can be used to modify the request context, the response, or both.
A few things to note about middlewares:
- They are executed in the order they are added.
- They can be used to modify the request context, the response, or both.
- They can be used to short-circuit the request handling process.
- They are purely synchronous and should not ideally not have side effects.
§Examples
pub struct RequestReceivedLogger {}
impl NgynMiddleware for RequestReceivedLogger {
async fn handle(cx: &mut NgynContext<'_>) {
println!("Request received: {:?}", cx.request());
}
}