Function hyper::service::make_service_fn[][src]

pub fn make_service_fn<F, Target, Ret>(f: F) -> MakeServiceFn<F> where
    F: FnMut(&Target) -> Ret,
    Ret: Future
Expand description

Create a MakeService from a function.

Example

use std::convert::Infallible;
use hyper::{Body, Request, Response, Server};
use hyper::server::conn::AddrStream;
use hyper::service::{make_service_fn, service_fn};

let addr = ([127, 0, 0, 1], 3000).into();

let make_svc = make_service_fn(|socket: &AddrStream| {
    let remote_addr = socket.remote_addr();
    async move {
        Ok::<_, Infallible>(service_fn(move |_: Request<Body>| async move {
            Ok::<_, Infallible>(
                Response::new(Body::from(format!("Hello, {}!", remote_addr)))
            )
        }))
    }
});

// Then bind and serve...
let server = Server::bind(&addr)
    .serve(make_svc);

// Finally, spawn `server` onto an Executor...
if let Err(e) = server.await {
    eprintln!("server error: {}", e);
}