Skip to content

Commit

Permalink
feat(server): add server::Serve that can use a shared Handle
Browse files Browse the repository at this point in the history
- 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`.
  • Loading branch information
seanmonstar committed Nov 7, 2017
1 parent 0844ded commit 39cf6ef
Show file tree
Hide file tree
Showing 3 changed files with 331 additions and 324 deletions.
54 changes: 21 additions & 33 deletions examples/multi_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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;
Expand All @@ -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()
Expand All @@ -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.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(|_| ()));

println!("Listening on http://{}", srv1.local_addr().unwrap());
println!("Listening on http://{}", srv2.local_addr().unwrap());
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();
}
Loading

0 comments on commit 39cf6ef

Please sign in to comment.