Function hyper::service::service_fn

source ·
pub fn service_fn<F, R, S>(f: F) -> ServiceFn<F, R>
where F: FnMut(Request<R>) -> S, S: Future,
Expand description

Create a Service from a function.

§Example

use hyper::{Body, Request, Response, Version};
use hyper::service::service_fn;

let service = service_fn(|req: Request<Body>| async move {
    if req.version() == Version::HTTP_11 {
        Ok(Response::new(Body::from("Hello World")))
    } else {
        // Note: it's usually better to return a Response
        // with an appropriate StatusCode instead of an Err.
        Err("not HTTP/1.1, abort connection")
    }
});