[][src]Crate spirit_hyper

Spirit extension for Hyper

This allows having Hyper servers auto-spawned from configuration. It is possible to put them on top of arbitrary stream-style IO objects (TcpStream, UdsStream, these wrapped in SSL...).

Tokio runtime

This uses the spirit-tokio crate under the hood. Similar drawback with initializing a runtime applies here too (see the spirit-tokio docs for details).

Examples

use hyper::{Body, Request, Response};
use hyper::server::Builder;
use hyper::service::service_fn_ok;
use serde::Deserialize;
use spirit::{Empty, Pipeline, Spirit};
use spirit::prelude::*;
use spirit_hyper::{BuildServer, HttpServer};

const DEFAULT_CONFIG: &str = r#"
[server]
port = 1234
"#;

#[derive(Default, Deserialize)]
struct Config {
    server: HttpServer,
}

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

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

fn main() {
    Spirit::<Empty, Config>::new()
        .config_defaults(DEFAULT_CONFIG)
        .with(
            // Let's build a http server as configured by the user
            Pipeline::new("listen")
                .extract_cfg(Config::server)
                // This is where we teach the server what it serves. It is the usual stuff from
                // hyper.
                .transform(BuildServer(|builder: Builder<_>, _cfg: &_, _name: &str| {
                    builder.serve(|| service_fn_ok(request))
                }))
        )
        .run(|spirit| {
            Ok(())
        });
}

Further examples are in the git repository.

Structs

Activate

A plumbing helper type.

BuildServer

A Transformation to turn a Builder into a Server.

HyperServer

A Fragment for hyper servers.

Type Definitions

HttpServer

A type alias for http (plain TCP) hyper server.