[][src]Struct routerify::RouterBuilder

pub struct RouterBuilder<B, E> { /* fields omitted */ }

Builder for the Router type.

This RouterBuilder<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.

Examples

use routerify::{Router, Middleware};
use hyper::{Response, Request, Body};

async fn home_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
    Ok(Response::new(Body::from("home")))
}

async fn upload_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
    Ok(Response::new(Body::from("upload")))
}

async fn some_pre_middleware_handler(req: Request<Body>) -> Result<Request<Body>, hyper::Error> {
    Ok(req)
}

// Use Router::builder() method to create a new RouterBuilder instance.
// We will use hyper::Body as response body type and hyper::Error as error type.
let router: Router<Body, hyper::Error> = Router::builder()
    .get("/", home_handler)
    .post("/upload", upload_handler)
    .middleware(Middleware::pre(some_pre_middleware_handler))
    .build()
    .unwrap();

Methods

impl<B: HttpBody + Send + Sync + Unpin + 'static, E: Error + Send + Sync + Unpin + 'static> RouterBuilder<B, E>[src]

pub fn new() -> RouterBuilder<B, E>[src]

Creates a new RouterBuilder instance with default options.

pub fn build(self) -> Result<Router<B, E>>[src]

Creates a new Router instance from the added configuration.

impl<B: HttpBody + Send + Sync + Unpin + 'static, E: Error + Send + Sync + Unpin + 'static> RouterBuilder<B, E>[src]

pub fn get<P, H, R>(self, path: P, handler: H) -> Self where
    P: Into<String>,
    H: FnMut(Request<Body>) -> R + Send + Sync + 'static,
    R: Future<Output = Result<Response<B>, E>> + Send + 'static, 
[src]

Adds a new route with GET method and the handler at the specified path.

Examples

use routerify::Router;
use hyper::{Response, Request, Body};

async fn home_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
    Ok(Response::new(Body::from("home")))
}

let router = Router::builder()
    .get("/", home_handler)
    .build()
    .unwrap();

pub fn get_or_head<P, H, R>(self, path: P, handler: H) -> Self where
    P: Into<String>,
    H: FnMut(Request<Body>) -> R + Send + Sync + 'static,
    R: Future<Output = Result<Response<B>, E>> + Send + 'static, 
[src]

Adds a new route with GET and HEAD methods and the handler at the specified path.

Examples

use routerify::Router;
use hyper::{Response, Request, Body};

async fn home_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
    Ok(Response::new(Body::from("home")))
}

let router = Router::builder()
    .get_or_head("/", home_handler)
    .build()
    .unwrap();

pub fn post<P, H, R>(self, path: P, handler: H) -> Self where
    P: Into<String>,
    H: FnMut(Request<Body>) -> R + Send + Sync + 'static,
    R: Future<Output = Result<Response<B>, E>> + Send + 'static, 
[src]

Adds a new route with POST method and the handler at the specified path.

Examples

use routerify::Router;
use hyper::{Response, Request, Body};

async fn file_upload_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
    Ok(Response::new(Body::from("File uploader")))
}

let router = Router::builder()
    .post("/upload", file_upload_handler)
    .build()
    .unwrap();

pub fn put<P, H, R>(self, path: P, handler: H) -> Self where
    P: Into<String>,
    H: FnMut(Request<Body>) -> R + Send + Sync + 'static,
    R: Future<Output = Result<Response<B>, E>> + Send + 'static, 
[src]

Adds a new route with PUT method and the handler at the specified path.

Examples

use routerify::Router;
use hyper::{Response, Request, Body};

async fn file_upload_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
    Ok(Response::new(Body::from("File uploader")))
}

let router = Router::builder()
    .put("/upload", file_upload_handler)
    .build()
    .unwrap();

pub fn delete<P, H, R>(self, path: P, handler: H) -> Self where
    P: Into<String>,
    H: FnMut(Request<Body>) -> R + Send + Sync + 'static,
    R: Future<Output = Result<Response<B>, E>> + Send + 'static, 
[src]

Adds a new route with DELETE method and the handler at the specified path.

Examples

use routerify::Router;
use hyper::{Response, Request, Body};

async fn delete_file_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
    Ok(Response::new(Body::from("Delete file")))
}

let router = Router::builder()
    .delete("/delete-file", delete_file_handler)
    .build()
    .unwrap();

pub fn head<P, H, R>(self, path: P, handler: H) -> Self where
    P: Into<String>,
    H: FnMut(Request<Body>) -> R + Send + Sync + 'static,
    R: Future<Output = Result<Response<B>, E>> + Send + 'static, 
[src]

Adds a new route with HEAD method and the handler at the specified path.

Examples

use routerify::Router;
use hyper::{Response, Request, Body};

async fn a_head_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
    Ok(Response::new(Body::empty()))
}

let router = Router::builder()
    .head("/fetch-data", a_head_handler)
    .build()
    .unwrap();

pub fn trace<P, H, R>(self, path: P, handler: H) -> Self where
    P: Into<String>,
    H: FnMut(Request<Body>) -> R + Send + Sync + 'static,
    R: Future<Output = Result<Response<B>, E>> + Send + 'static, 
[src]

Adds a new route with TRACE method and the handler at the specified path.

Examples

use routerify::Router;
use hyper::{Response, Request, Body};

async fn trace_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
    Ok(Response::new(Body::empty()))
}

let router = Router::builder()
    .trace("/abc", trace_handler)
    .build()
    .unwrap();

pub fn connect<P, H, R>(self, path: P, handler: H) -> Self where
    P: Into<String>,
    H: FnMut(Request<Body>) -> R + Send + Sync + 'static,
    R: Future<Output = Result<Response<B>, E>> + Send + 'static, 
[src]

Adds a new route with CONNECT method and the handler at the specified path.

Examples

use routerify::Router;
use hyper::{Response, Request, Body};

async fn connect_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
    Ok(Response::new(Body::empty()))
}

let router = Router::builder()
    .connect("/abc", connect_handler)
    .build()
    .unwrap();

pub fn patch<P, H, R>(self, path: P, handler: H) -> Self where
    P: Into<String>,
    H: FnMut(Request<Body>) -> R + Send + Sync + 'static,
    R: Future<Output = Result<Response<B>, E>> + Send + 'static, 
[src]

Adds a new route with PATCH method and the handler at the specified path.

Examples

use routerify::Router;
use hyper::{Response, Request, Body};

async fn update_data_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
    Ok(Response::new(Body::from("Data updater")))
}

let router = Router::builder()
    .patch("/update-data", update_data_handler)
    .build()
    .unwrap();

pub fn options<P, H, R>(self, path: P, handler: H) -> Self where
    P: Into<String>,
    H: FnMut(Request<Body>) -> R + Send + Sync + 'static,
    R: Future<Output = Result<Response<B>, E>> + Send + 'static, 
[src]

Adds a new route with OPTIONS method and the handler at the specified path.

Examples

use routerify::Router;
use hyper::{Response, Request, Body};

async fn options_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
    Ok(Response::new(Body::empty()))
}

let router = Router::builder()
    .options("/abc", options_handler)
    .build()
    .unwrap();

pub fn any<H, R>(self, handler: H) -> Self where
    H: FnMut(Request<Body>) -> R + Send + Sync + 'static,
    R: Future<Output = Result<Response<B>, E>> + Send + 'static, 
[src]

Adds a new route with any method type and the handler at the /* path. It will accept any kind of request. It can be used to send response for any non-existing routes i.e. for 404 pages.

Examples

use routerify::Router;
use hyper::{Response, Request, Body, StatusCode};

async fn home_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
    Ok(Response::new(Body::from("home")))
}

async fn handler_404(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
    Ok(
        Response::builder()
         .status(StatusCode::NOT_FOUND)
         .body(Body::from("NOT FOUND"))
         .unwrap()
     )
}

let router = Router::builder()
    .get("/home", home_handler)
    .any(handler_404)
    .build()
    .unwrap();

pub fn add<P, H, R>(self, path: P, methods: Vec<Method>, handler: H) -> Self where
    P: Into<String>,
    H: FnMut(Request<Body>) -> R + Send + Sync + 'static,
    R: Future<Output = Result<Response<B>, E>> + Send + 'static, 
[src]

Adds a new route with the specified method(s) and the handler at the specified path. It can be used to define routes with multiple method types.

Examples

use routerify::Router;
use hyper::{Response, Request, Body, StatusCode, Method};

async fn cart_checkout_handler(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
    Ok(Response::new(Body::from("You shopping cart is being checking out")))
}

let router = Router::builder()
    .add("/checkout", vec![Method::GET, Method::POST], cart_checkout_handler)
    .build()
    .unwrap();

pub fn scope<P>(self, path: P, router: Router<B, E>) -> Self where
    P: Into<String>, 
[src]

It mounts a router onto another router. It can be very useful when you want to write modular routing logic.

Examples

use routerify::Router;
use hyper::{Response, Request, Body};

mod api {
    use routerify::Router;
    use hyper::{Response, Request, Body};

    pub fn router() -> Router<Body, hyper::Error> {
        Router::builder()
         .get("/users", |req| async move { Ok(Response::new(Body::from("User list"))) })
         .get("/books", |req| async move { Ok(Response::new(Body::from("Book list"))) })
         .build()
         .unwrap()
    }
}

let router = Router::builder()
    // Now, mount the api router at `/api` path.
    .scope("/api", api::router())
    .build()
    .unwrap();

Now, the app can handle requests on: /api/users and /api/books paths.

impl<B: HttpBody + Send + Sync + Unpin + 'static, E: Error + Send + Sync + Unpin + 'static> RouterBuilder<B, E>[src]

pub fn middleware(self, m: Middleware<B, E>) -> Self[src]

Adds a single middleware. A pre middleware can be created by Middleware::pre method and a post middleware can be created by Middleware::post method.

Examples

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

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

pub fn err_handler<H, R>(self, handler: H) -> Self where
    H: FnMut(Error) -> R + Send + Sync + 'static,
    R: Future<Output = Response<B>> + Send + 'static, 
[src]

Adds a handler to handle any error raised by the routes or any middlewares. Please refer to Error Handling section for more info.

Trait Implementations

impl<B: HttpBody + Send + Sync + Unpin + 'static, E: Error + Send + Sync + Unpin + 'static> Default for RouterBuilder<B, E>[src]

Auto Trait Implementations

impl<B, E> !RefUnwindSafe for RouterBuilder<B, E>

impl<B, E> Send for RouterBuilder<B, E>

impl<B, E> Sync for RouterBuilder<B, E>

impl<B, E> Unpin for RouterBuilder<B, E>

impl<B, E> !UnwindSafe for RouterBuilder<B, E>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.