Function spirit_hyper::server

source ·
pub fn server<R, O, C, CMS, B, E, ME, S, F>(
    configured_make_service: CMS
) -> impl ResourceConsumer<HyperServer<R>, O, C>where
    R: ResourceConfig<O, C>,
    R::Resource: IntoIncoming,
    <R::Resource as IntoIncoming>::Connection: AsyncRead + AsyncWrite,
    CMS: ConfiguredMakeService<O, C, HyperServer<R>>,
    CMS::MakeService: for<'a> MakeService<&'a <R::Resource as IntoIncoming>::Connection, ReqBody = Body, Error = E, MakeError = ME, Service = S, Future = F, ResBody = B> + Send + Sync + 'static,
    E: Into<Box<dyn Error + Send + Sync>>,
    ME: Into<Box<dyn Error + Send + Sync>>,
    S: Service<ReqBody = Body, ResBody = B, Error = E> + Send + 'static,
    S::Future: Send,
    F: Future<Item = S, Error = ME> + Send + 'static,
    B: Payload,
Expand description

Creates a ResourceConsumer from a ConfiguredMakeService.

This is the lowest level constructor of the hyper resource consumers, when the full flexibility is needed.

Examples

extern crate hyper;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate spirit;
extern crate spirit_hyper;
extern crate spirit_tokio;

use hyper::{Body, Request, Response};
use spirit::{Empty, Spirit};
use spirit_hyper::HttpServer;

#[derive(Default, Deserialize)]
struct Config {
    #[serde(default)]
    server: Vec<HttpServer>,
}

impl Config {
    fn server(&self) -> Vec<HttpServer> {
        self.server.clone()
    }
}

fn request(_req: Request<Body>) -> Response<Body> {
    Response::new(Body::from("Hello world\n"))
}

fn main() {
    Spirit::<Empty, Config>::new()
        .with(spirit_tokio::resources(
            Config::server,
            spirit_hyper::server(|_spirit: &_, _cfg: &_, _resource: &_, _name: &str| {
                || hyper::service::service_fn_ok(request)
            }),
            "server",
        ))
        .run(|spirit| {
            Ok(())
        });
}