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.
		
	
		
			
				
	
	
		
			58 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			58 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| //#![deny(warnings)]
 | |
| extern crate futures;
 | |
| extern crate hyper;
 | |
| extern crate pretty_env_logger;
 | |
| #[macro_use]
 | |
| extern crate log;
 | |
| 
 | |
| use hyper::{Get, Post, StatusCode};
 | |
| use hyper::header::ContentLength;
 | |
| use hyper::server::{Server, Service, Request, Response};
 | |
| 
 | |
| 
 | |
| static INDEX: &'static [u8] = b"Try POST /echo";
 | |
| 
 | |
| #[derive(Clone, Copy)]
 | |
| struct Echo;
 | |
| 
 | |
| impl Service for Echo {
 | |
|     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(match (req.method(), req.path()) {
 | |
|             (&Get, Some("/")) | (&Get, Some("/echo")) => {
 | |
|                 Response::new()
 | |
|                     .with_header(ContentLength(INDEX.len() as u64))
 | |
|                     .with_body(INDEX)
 | |
|             },
 | |
|             (&Post, Some("/echo")) => {
 | |
|                 let mut res = Response::new();
 | |
|                 if let Some(len) = req.headers().get::<ContentLength>() {
 | |
|                     res.headers_mut().set(len.clone());
 | |
|                 }
 | |
|                 res.with_body(req.body())
 | |
|             },
 | |
|             _ => {
 | |
|                 Response::new()
 | |
|                     .with_status(StatusCode::NotFound)
 | |
|             }
 | |
|         })
 | |
|     }
 | |
| 
 | |
| }
 | |
| 
 | |
| 
 | |
| 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();
 | |
| }
 |