Module service

Source
Expand description

The Hyper service module.

It provides a Hyper Service implementation intended to work with the hyper::server::Builder.

The service allows to bind a Middlewares of middlewares.

§Example

use hyper::Server;
use hyper_middleware::{
    async_trait, Body, Handler, Request, Response, Middlewares, Result, Service,
};

struct Application {}

#[async_trait]
impl Handler for Application {
    async fn handle(&self, _req: &mut Request) -> Result<Response> {
        // Create a response and send it back to the middlewares chain
        Ok(Response::new(Body::from("¡Hola!")))
    }
}

#[tokio::main(flavor = "multi_thread")]
async fn main() -> Result {
    let mut middlewares = Middlewares::new(Application {});

    let service = Service::new(middlewares);

    let addr = ([127, 0, 0, 1], 8087).into();
    let server = Server::bind(&addr).serve(service);

    println!("Listening on http://{}", addr);

    // server.await?;

    Ok(())
}

Structs§

Service
A Hyper Service entry point which hosts a Handler.