Struct RouterService

Source
pub struct RouterService<B, E> { /* private fields */ }
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§

Source§

impl<B: HttpBody + Send + Sync + 'static, E: Into<Box<dyn Error + Send + Sync>> + 'static> RouterService<B, E>

Source

pub fn new(router: Router<B, E>) -> Result<RouterService<B, E>>

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

Trait Implementations§

Source§

impl<B: Debug, E: Debug> Debug for RouterService<B, E>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<B: HttpBody + Send + Sync + 'static, E: Into<Box<dyn Error + Send + Sync>> + 'static> Service<&AddrStream> for RouterService<B, E>

Source§

type Response = RequestService<B, E>

Responses given by the service.
Source§

type Error = Infallible

Errors produced by the service.
Source§

type Future = Ready<Result<<RouterService<B, E> as Service<&AddrStream>>::Response, <RouterService<B, E> as Service<&AddrStream>>::Error>>

The future response value.
Source§

fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>

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

fn call(&mut self, conn: &AddrStream) -> Self::Future

Process the request and return the response asynchronously. Read more

Auto Trait Implementations§

§

impl<B, E> Freeze for RouterService<B, E>

§

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

§

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

§

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

§

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

§

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

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

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

fn in_current_span(self) -> Instrumented<Self>

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

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more