Struct routerify::RouterService[][src]

pub struct RouterService<B, E> { /* fields omitted */ }
Expand description

A Service to process incoming requests.

This RouterService<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 hyper::{Body, Request, Response, Server};
use routerify::{Router, RouterService};
use std::convert::Infallible;
use std::net::SocketAddr;

// A handler for "/" page.
async fn home(_: Request<Body>) -> Result<Response<Body>, Infallible> {
    Ok(Response::new(Body::from("Home page")))
}

fn router() -> Router<Body, Infallible> {
    Router::builder()
        .get("/", home)
        .build()
        .unwrap()
}

#[tokio::main]
async fn main() {
    let router = router();

    // Create a Service from the router above to handle incoming requests.
    let service = RouterService::new(router).unwrap();

    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));

    // Create a server by passing the created service to `.serve` method.
    let server = Server::bind(&addr).serve(service);

    println!("App is running on: {}", addr);
    if let Err(err) = server.await {
        eprintln!("Server error: {}", err);
   }
}

Implementations

Creates a new service with the provided router and it’s ready to be used with the hyper serve method.

Trait Implementations

Formats the value using the given formatter. Read more

Responses given by the service.

Errors produced by the service.

The future response value.

Returns Poll::Ready(Ok(())) when the service is able to process requests. Read more

Process the request and return the response asynchronously. 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.