Enum routerify::Middleware[][src]

pub enum Middleware<B, E> {
    Pre(PreMiddleware<E>),
    Post(PostMiddleware<B, E>),
}
Expand description

Enum type for all the middleware types. Please refer to the Middleware for more info.

This Middleware<B, E> type accepts two type parameters: B and E.

  • The B represents the response body type which will be used by route handlers and the middlewares and this body type must implement the HttpBody trait. For an instance, B could be hyper::Body type.
  • The E represents any error type which will be used by route handlers and the middlewares. This error type must implement the std::error::Error.

Variants

Variant for the pre middleware. Refer to Pre Middleware for more info.

Post(PostMiddleware<B, E>)

Variant for the post middleware. Refer to Post Middleware for more info.

Implementations

Creates a pre middleware with a handler at the /* path.

Examples

use routerify::{Router, Middleware};
use hyper::{Request, Body};
use std::convert::Infallible;

let router = Router::builder()
     .middleware(Middleware::pre(|req| async move { /* Do some operations */ Ok(req) }))
     .build()
     .unwrap();

Creates a post middleware with a handler at the /* path.

Examples

use routerify::{Router, Middleware};
use hyper::{Response, Body};
use std::convert::Infallible;

let router = Router::builder()
     .middleware(Middleware::post(|res| async move { /* Do some operations */ Ok(res) }))
     .build()
     .unwrap();

Creates a post middleware which can access request info e.g. headers, method, uri etc. It should be used when the post middleware trandforms the response based on the request information.

Examples

use routerify::{Router, Middleware, PostMiddleware, RequestInfo};
use hyper::{Response, Body};
use std::convert::Infallible;

async fn post_middleware_with_info_handler(res: Response<Body>, req_info: RequestInfo) -> Result<Response<Body>, Infallible> {
    let headers = req_info.headers();
     
    // Do some response transformation based on the request headers, method etc.
     
    Ok(res)
}

let router = Router::builder()
     .middleware(Middleware::post_with_info(post_middleware_with_info_handler))
     .build()
     .unwrap();

Create a pre middleware with a handler at the specified path.

Examples

use routerify::{Router, Middleware};
use hyper::{Request, Body};
use std::convert::Infallible;

let router = Router::builder()
     .middleware(Middleware::pre_with_path("/my-path", |req| async move { /* Do some operations */ Ok(req) }).unwrap())
     .build()
     .unwrap();

Creates a post middleware with a handler at the specified path.

Examples

use routerify::{Router, Middleware};
use hyper::{Response, Body};
use std::convert::Infallible;

let router = Router::builder()
     .middleware(Middleware::post_with_path("/my-path", |res| async move { /* Do some operations */ Ok(res) }).unwrap())
     .build()
     .unwrap();

Creates a post middleware which can access request info e.g. headers, method, uri etc. It should be used when the post middleware trandforms the response based on the request information.

Examples

use routerify::{Router, Middleware, PostMiddleware, RequestInfo};
use hyper::{Response, Body};
use std::convert::Infallible;

async fn post_middleware_with_info_handler(res: Response<Body>, req_info: RequestInfo) -> Result<Response<Body>, Infallible> {
    let _headers = req_info.headers();
     
    // Do some response transformation based on the request headers, method etc.
     
    Ok(res)
}

let router = Router::builder()
     .middleware(Middleware::post_with_info_with_path("/abc", post_middleware_with_info_handler).unwrap())
     .build()
     .unwrap();

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.