Struct routerify::RouterBuilder [−][src]
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();
Implementations
impl<B: HttpBody + Send + Sync + 'static, E: Into<Box<dyn Error + Send + Sync>> + 'static> RouterBuilder<B, E>
[src]
impl<B: HttpBody + Send + Sync + 'static, E: Into<Box<dyn Error + Send + Sync>> + 'static> RouterBuilder<B, E>
[src]impl<B: HttpBody + Send + Sync + 'static, E: Into<Box<dyn Error + Send + Sync>> + 'static> RouterBuilder<B, E>
[src]
impl<B: HttpBody + Send + Sync + 'static, E: Into<Box<dyn Error + Send + Sync>> + 'static> RouterBuilder<B, E>
[src]pub fn get<P, H, R>(self, path: P, handler: H) -> Self where
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<B>, E>> + Send + 'static,
[src]
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<B>, E>> + Send + 'static,
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: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<B>, E>> + Send + 'static,
[src]
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<B>, E>> + Send + 'static,
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: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<B>, E>> + Send + 'static,
[src]
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<B>, E>> + Send + 'static,
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: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<B>, E>> + Send + 'static,
[src]
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<B>, E>> + Send + 'static,
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: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<B>, E>> + Send + 'static,
[src]
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<B>, E>> + Send + 'static,
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: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<B>, E>> + Send + 'static,
[src]
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<B>, E>> + Send + 'static,
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: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<B>, E>> + Send + 'static,
[src]
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<B>, E>> + Send + 'static,
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: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<B>, E>> + Send + 'static,
[src]
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<B>, E>> + Send + 'static,
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: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<B>, E>> + Send + 'static,
[src]
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<B>, E>> + Send + 'static,
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: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<B>, E>> + Send + 'static,
[src]
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<B>, E>> + Send + 'static,
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: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<B>, E>> + Send + 'static,
[src]
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<B>, E>> + Send + 'static,
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 any_method<H, R, P>(self, path: P, handler: H) -> Self where
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<B>, E>> + Send + 'static,
[src]
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<B>, E>> + Send + 'static,
Adds a new route with any method type 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() // It will accept requests at any method type at the specified path. .any_method("/", home_handler) .build() .unwrap();
pub fn add<P, H, R>(self, path: P, methods: Vec<Method>, handler: H) -> Self where
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<B>, E>> + Send + 'static,
[src]
P: Into<String>,
H: Fn(Request<Body>) -> R + Send + Sync + 'static,
R: Future<Output = Result<Response<B>, E>> + Send + 'static,
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]
P: Into<String>,
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 + 'static, E: Into<Box<dyn Error + Send + Sync>> + 'static> RouterBuilder<B, E>
[src]
impl<B: HttpBody + Send + Sync + 'static, E: Into<Box<dyn Error + Send + Sync>> + '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 data<T: Send + Sync + 'static>(self, data: T) -> Self
[src]
Specify app data to be shared across route handlers, middlewares and the error handler.
Please refer to the Data and State Sharing for more info.
pub fn err_handler<H, R>(self, handler: H) -> Self where
H: Fn(RouteError) -> R + Send + Sync + 'static,
R: Future<Output = Response<B>> + Send + 'static,
[src]
H: Fn(RouteError) -> R + Send + Sync + 'static,
R: Future<Output = Response<B>> + Send + 'static,
Adds a handler to handle any error raised by the routes or any middlewares. Please refer to Error Handling section for more info.
pub fn err_handler_with_info<H, R>(self, handler: H) -> Self where
H: Fn(RouteError, RequestInfo) -> R + Send + Sync + 'static,
R: Future<Output = Response<B>> + Send + 'static,
[src]
H: Fn(RouteError, RequestInfo) -> R + Send + Sync + 'static,
R: Future<Output = Response<B>> + Send + 'static,
Adds a handler to handle any error raised by the routes or any middlewares.
Here, the handler also access request info e.g. headers, method, uri etc to generate response based on the request information.
Please refer to Error Handling section for more info.
Trait Implementations
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> Instrument for T
[src]
impl<T> Instrument for T
[src]