[−][src]Trait roa::Middleware
Middleware
Build-in middlewares
- Functional middleware
A functional middleware is an async function with signature:
async fn(&mut Context, Next<'_>) -> Result
.
use roa_core::{App, Context, Next, Result}; async fn middleware(ctx: &mut Context, next: Next<'_>) -> Result { next.await } let app = App::new().gate(middleware);
- Blank middleware
()
is a blank middleware, it just calls the next middleware or endpoint.
let app = roa_core::App::new().gate(());
Custom middleware
You can implement custom Middleware
for other types.
use roa_core::{App, Middleware, Context, Next, Result, async_trait}; use std::sync::Arc; use std::time::Instant; struct Logger; #[async_trait(?Send)] impl <'a> Middleware<'a> for Logger { async fn handle(&'a self, ctx: &'a mut Context, next: Next<'a>) -> Result { let start = Instant::now(); let result = next.await; println!("time elapsed: {}ms", start.elapsed().as_millis()); result } } let app = App::new().gate(Logger);
Required methods
#[must_use]fn handle<'async_trait>(
&'a self,
ctx: &'a mut Context<S>,
next: &'a mut (dyn Future<Output = Result<(), Status>> + 'a + Unpin)
) -> Pin<Box<dyn Future<Output = Result<(), Status>> + 'async_trait>> where
'a: 'async_trait,
Self: 'async_trait,
&'a self,
ctx: &'a mut Context<S>,
next: &'a mut (dyn Future<Output = Result<(), Status>> + 'a + Unpin)
) -> Pin<Box<dyn Future<Output = Result<(), Status>> + 'async_trait>> where
'a: 'async_trait,
Self: 'async_trait,
Handle context and next, return status.
Implementations on Foreign Types
impl<'a, S> Middleware<'a, S> for ()
[src]
blank middleware.
fn handle<'async_trait>(
&'a self,
_ctx: &'a mut Context<S>,
next: &'a mut (dyn Future<Output = Result<(), Status>> + 'a + Unpin)
) -> Pin<Box<dyn Future<Output = Result<(), Status>> + 'async_trait>> where
'a: 'async_trait,
(): 'async_trait,
[src]
&'a self,
_ctx: &'a mut Context<S>,
next: &'a mut (dyn Future<Output = Result<(), Status>> + 'a + Unpin)
) -> Pin<Box<dyn Future<Output = Result<(), Status>> + 'async_trait>> where
'a: 'async_trait,
(): 'async_trait,
Implementors
impl<'a, S> Middleware<'a, S> for Compress
[src]
fn handle<'async_trait>(
&'a self,
ctx: &'a mut Context<S>,
next: Next<'a>
) -> Pin<Box<dyn Future<Output = Result> + 'async_trait>> where
'a: 'async_trait,
Self: 'async_trait,
[src]
&'a self,
ctx: &'a mut Context<S>,
next: Next<'a>
) -> Pin<Box<dyn Future<Output = Result> + 'async_trait>> where
'a: 'async_trait,
Self: 'async_trait,
impl<'a, S> Middleware<'a, S> for Cors
[src]
fn handle<'async_trait>(
&'a self,
ctx: &'a mut Context<S>,
next: Next<'a>
) -> Pin<Box<dyn Future<Output = Result> + 'async_trait>> where
'a: 'async_trait,
Self: 'async_trait,
[src]
&'a self,
ctx: &'a mut Context<S>,
next: Next<'a>
) -> Pin<Box<dyn Future<Output = Result> + 'async_trait>> where
'a: 'async_trait,
Self: 'async_trait,
impl<'a, S> Middleware<'a, S> for JwtGuard
[src]
fn handle<'async_trait>(
&'a self,
ctx: &'a mut Context<S>,
next: Next<'a>
) -> Pin<Box<dyn Future<Output = Result> + 'async_trait>> where
'a: 'async_trait,
Self: 'async_trait,
[src]
&'a self,
ctx: &'a mut Context<S>,
next: Next<'a>
) -> Pin<Box<dyn Future<Output = Result> + 'async_trait>> where
'a: 'async_trait,
Self: 'async_trait,
impl<'a, S> Middleware<'a, S> for Shared<S> where
S: 'static,
[src]
S: 'static,
fn handle<'async_trait>(
&'a self,
ctx: &'a mut Context<S>,
next: &'a mut (dyn Future<Output = Result<(), Status>> + 'a + Unpin)
) -> Pin<Box<dyn Future<Output = Result<(), Status>> + 'async_trait>> where
'a: 'async_trait,
Shared<S>: 'async_trait,
[src]
&'a self,
ctx: &'a mut Context<S>,
next: &'a mut (dyn Future<Output = Result<(), Status>> + 'a + Unpin)
) -> Pin<Box<dyn Future<Output = Result<(), Status>> + 'async_trait>> where
'a: 'async_trait,
Shared<S>: 'async_trait,
impl<'a, S, T, F> Middleware<'a, S> for T where
F: 'a + Future<Output = Result<(), Status>>,
S: 'a,
T: 'static + Send + Sync + Fn(&'a mut Context<S>, &'a mut (dyn Future<Output = Result<(), Status>> + 'a + Unpin)) -> F,
[src]
F: 'a + Future<Output = Result<(), Status>>,
S: 'a,
T: 'static + Send + Sync + Fn(&'a mut Context<S>, &'a mut (dyn Future<Output = Result<(), Status>> + 'a + Unpin)) -> F,
fn handle<'async_trait>(
&'a self,
ctx: &'a mut Context<S>,
next: &'a mut (dyn Future<Output = Result<(), Status>> + 'a + Unpin)
) -> Pin<Box<dyn Future<Output = Result<(), Status>> + 'async_trait>> where
'a: 'async_trait,
T: 'async_trait,
[src]
&'a self,
ctx: &'a mut Context<S>,
next: &'a mut (dyn Future<Output = Result<(), Status>> + 'a + Unpin)
) -> Pin<Box<dyn Future<Output = Result<(), Status>> + 'async_trait>> where
'a: 'async_trait,
T: 'async_trait,
impl<'a, S, T, U> Middleware<'a, S> for Chain<T, U> where
T: for<'b> Middleware<'b, S>,
U: Middleware<'a, S>,
[src]
T: for<'b> Middleware<'b, S>,
U: Middleware<'a, S>,