feat(server): add server::Serve that can use a shared Handle

- Adds `Http::serve_addr_handle` which will bind to an address with a
  provided `Handle`, and return a `Serve`.
- Adds `server::Serve` which is a `Stream` of incoming `Connection`s
  being bound by a `NewService`.
- Renames `Http::no_proto` to `Http::serve_connection`.
This commit is contained in:
Sean McArthur
2017-10-30 14:17:43 -07:00
parent 0844dede19
commit 39cf6ef7d2
3 changed files with 346 additions and 339 deletions

View File

@@ -4,6 +4,7 @@ extern crate futures;
extern crate tokio_core;
extern crate pretty_env_logger;
use futures::{Future, Stream};
use futures::future::FutureResult;
use hyper::{Get, StatusCode};
@@ -14,10 +15,9 @@ use hyper::server::{Http, Service, Request, Response};
static INDEX1: &'static [u8] = b"The 1st service!";
static INDEX2: &'static [u8] = b"The 2nd service!";
struct Service1;
struct Service2;
struct Srv(&'static [u8]);
impl Service for Service1 {
impl Service for Srv {
type Request = Request;
type Response = Response;
type Error = hyper::Error;
@@ -27,30 +27,8 @@ impl Service for Service1 {
futures::future::ok(match (req.method(), req.path()) {
(&Get, "/") => {
Response::new()
.with_header(ContentLength(INDEX1.len() as u64))
.with_body(INDEX1)
},
_ => {
Response::new()
.with_status(StatusCode::NotFound)
}
})
}
}
impl Service for Service2 {
type Request = Request;
type Response = Response;
type Error = hyper::Error;
type Future = FutureResult<Response, hyper::Error>;
fn call(&self, req: Request) -> Self::Future {
futures::future::ok(match (req.method(), req.path()) {
(&Get, "/") => {
Response::new()
.with_header(ContentLength(INDEX2.len() as u64))
.with_body(INDEX2)
.with_header(ContentLength(self.0.len() as u64))
.with_body(self.0)
},
_ => {
Response::new()
@@ -70,13 +48,23 @@ fn main() {
let mut core = Core::new().unwrap();
let handle = core.handle();
let srv1 = Http::new().bind_handle(&addr1,|| Ok(Service1), &handle).unwrap();
let srv2 = Http::new().bind_handle(&addr2,|| Ok(Service2), &handle).unwrap();
let srv1 = Http::new().serve_addr_handle(&addr1, &handle, || Ok(Srv(INDEX1))).unwrap();
let srv2 = Http::new().serve_addr_handle(&addr2, &handle, || Ok(Srv(INDEX2))).unwrap();
println!("Listening on http://{}", srv1.local_addr().unwrap());
println!("Listening on http://{}", srv2.local_addr().unwrap());
println!("Listening on http://{}", srv1.incoming_ref().local_addr());
println!("Listening on http://{}", srv2.incoming_ref().local_addr());
let handle1 = handle.clone();
handle.spawn(srv1.for_each(move |conn| {
handle1.spawn(conn.map(|_| ()).map_err(|err| println!("srv1 error: {:?}", err)));
Ok(())
}).map_err(|_| ()));
let handle2 = handle.clone();
handle.spawn(srv2.for_each(move |conn| {
handle2.spawn(conn.map(|_| ()).map_err(|err| println!("srv2 error: {:?}", err)));
Ok(())
}).map_err(|_| ()));
handle.spawn(srv1.shutdown_signal(futures::future::empty::<(), ()>()));
handle.spawn(srv2.shutdown_signal(futures::future::empty::<(), ()>()));
core.run(futures::future::empty::<(), ()>()).unwrap();
}