refactor(server): expose Http that implements ServerProto

The main changes are:

* The entry point is how `Http`, the implementation of `ServerProto`.
  This type has a `new` constructor as well as builder methods to
  configure it.

* A high-level entry point of `Http::bind` was added which returns a
  `Server`. Binding a protocol to a port requires a socket address
  (where to bind) as well as the instance of `NewService`. Internally
  this creates a core and a TCP listener.

* The returned `Server` has a few methods to learn about itself, e.g.
  `local_addr` and `handle`, but mainly has two methods: `run` and
  `run_until`.

* The `Server::run` entry point will execute a server infinitely, never
  having it exit.

* The `Server::run_until` method is intended as a graceful shutdown
  mechanism. When the provided future resolves the server stops
  accepting connections immediately and then waits for a fixed period of
  time for all active connections to get torn down, after which the
  whole server is torn down anyway.

* Finally a `Http::bind_connection` method exists as a low-level entry
  point to spawning a server connection. This is used by `Server::run`
  as is intended for external use in other event loops if necessary or
  otherwise low-level needs.

BREAKING CHANGE: `Server` is no longer the pimary entry point. Instead,
  an `Http` type is created  and then either `bind` to receiver a `Server`,
  or it can be passed to other Tokio things.
This commit is contained in:
Alex Crichton
2017-01-18 14:09:20 -08:00
committed by Sean McArthur
parent 39a53fcd33
commit f45e9c8e4f
6 changed files with 395 additions and 281 deletions

View File

@@ -5,7 +5,7 @@ extern crate pretty_env_logger;
//extern crate num_cpus;
use hyper::header::{ContentLength, ContentType};
use hyper::server::{Server, Service, Request, Response};
use hyper::server::{Http, Service, Request, Response};
static PHRASE: &'static [u8] = b"Hello World!";
@@ -31,9 +31,7 @@ impl Service for Hello {
fn main() {
pretty_env_logger::init().unwrap();
let addr = "127.0.0.1:3000".parse().unwrap();
let _server = Server::standalone(|tokio| {
Server::http(&addr, tokio)?
.handle(|| Ok(Hello), tokio)
}).unwrap();
println!("Listening on http://{}", addr);
let server = Http::new().bind(&addr, || Ok(Hello)).unwrap();
println!("Listening on http://{}", server.local_addr().unwrap());
server.run().unwrap();
}

View File

@@ -7,8 +7,7 @@ extern crate log;
use hyper::{Get, Post, StatusCode};
use hyper::header::ContentLength;
use hyper::server::{Server, Service, Request, Response};
use hyper::server::{Http, Service, Request, Response};
static INDEX: &'static [u8] = b"Try POST /echo";
@@ -48,10 +47,8 @@ impl Service for Echo {
fn main() {
pretty_env_logger::init().unwrap();
let addr = "127.0.0.1:1337".parse().unwrap();
let (listening, server) = Server::standalone(|tokio| {
Server::http(&addr, tokio)?
.handle(|| Ok(Echo), tokio)
}).unwrap();
println!("Listening on http://{}", listening);
server.run();
let server = Http::new().bind(&addr, || Ok(Echo)).unwrap();
println!("Listening on http://{}", server.local_addr().unwrap());
server.run().unwrap();
}