Files
hyper/examples/hello.rs
Sean McArthur 2d2d5574a6 feat(lib): redesign API to use Futures and Tokio
There are many changes involved with this, but let's just talk about
user-facing changes.

- Creating a `Client` and `Server` now needs a Tokio `Core` event loop
to attach to.
- `Request` and `Response` both no longer implement the
`std::io::{Read,Write}` traits, but instead represent their bodies as a
`futures::Stream` of items, where each item is a `Chunk`.
- The `Client.request` method now takes a `Request`, instead of being
used as a builder, and returns a `Future` that resolves to `Response`.
- The `Handler` trait for servers is no more, and instead the Tokio
`Service` trait is used. This allows interoperability with generic
middleware.

BREAKING CHANGE: A big sweeping set of breaking changes.
2017-01-16 10:44:27 -08:00

40 lines
1.0 KiB
Rust

#![deny(warnings)]
extern crate hyper;
extern crate futures;
extern crate pretty_env_logger;
//extern crate num_cpus;
use hyper::header::{ContentLength, ContentType};
use hyper::server::{Server, Service, Request, Response};
static PHRASE: &'static [u8] = b"Hello World!";
#[derive(Clone, Copy)]
struct Hello;
impl Service for Hello {
type Request = Request;
type Response = Response;
type Error = hyper::Error;
type Future = ::futures::Finished<Response, hyper::Error>;
fn call(&self, _req: Request) -> Self::Future {
::futures::finished(
Response::new()
.with_header(ContentLength(PHRASE.len() as u64))
.with_header(ContentType::plaintext())
.with_body(PHRASE)
)
}
}
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);
}