[][src]Trait roa::Middleware

pub trait Middleware<'a, S = ()>: 'static + Send + Sync {
#[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
; }

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, 

Handle context and next, return status.

Loading content...

Implementations on Foreign Types

impl<'a, S> Middleware<'a, S> for ()[src]

blank middleware.

Loading content...

Implementors

impl<'a, S> Middleware<'a, S> for Compress[src]

impl<'a, S> Middleware<'a, S> for Cors[src]

impl<'a, S> Middleware<'a, S> for JwtGuard[src]

impl<'a, S> Middleware<'a, S> for Shared<S> where
    S: 'static, 
[src]

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]

impl<'a, S, T, U> Middleware<'a, S> for Chain<T, U> where
    T: for<'b> Middleware<'b, S>,
    U: Middleware<'a, S>, 
[src]

Loading content...