Skip to content

Commit 5fddaa2

Browse files
author
Fuyang Liu
committed
Try update example echo - WIP
1 parent 8d4ae27 commit 5fddaa2

File tree

1 file changed

+37
-31
lines changed

1 file changed

+37
-31
lines changed

examples/echo.rs

+37-31
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
1+
#![feature(async_await)]
12
#![deny(warnings)]
2-
extern crate futures;
3+
34
extern crate hyper;
45

5-
use futures::future;
6-
use hyper::rt::{Future, Stream};
7-
use hyper::service::service_fn;
8-
use hyper::{Body, Method, Request, Response, Server, StatusCode};
6+
use futures_util::stream::StreamExt; // TODO: is this correct or?
97

10-
/// We need to return different futures depending on the route matched,
11-
/// and we can do that with an enum, such as `futures::Either`, or with
12-
/// trait objects.
13-
///
14-
/// A boxed Future (trait object) is used as it is easier to understand
15-
/// and extend with more types. Advanced users could switch to `Either`.
16-
type BoxFut = Box<dyn Future<Item = Response<Body>, Error = hyper::Error> + Send>;
8+
use hyper::{Body, Method, Request, Response, Server, StatusCode};
9+
use hyper::service::{make_service_fn, service_fn};
10+
use hyper::body::Chunk;
1711

1812
/// This is our service handler. It receives a Request, routes on its
1913
/// path, and returns a Future of a Response.
20-
fn echo(req: Request<Body>) -> BoxFut {
14+
async fn echo(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
2115
let mut response = Response::new(Body::empty());
2216

2317
match (req.method(), req.uri().path()) {
@@ -34,13 +28,15 @@ fn echo(req: Request<Body>) -> BoxFut {
3428
// Convert to uppercase before sending back to client.
3529
(&Method::POST, "/echo/uppercase") => {
3630
let mapping = req.into_body().map(|chunk| {
37-
chunk
31+
let chunk= chunk.unwrap();
32+
let x = chunk
3833
.iter()
3934
.map(|byte| byte.to_ascii_uppercase())
40-
.collect::<Vec<u8>>()
41-
});
35+
.collect::<Vec<_>>();
36+
x
37+
}).collect::<Vec<_>>().await;
4238

43-
*response.body_mut() = Body::wrap_stream(mapping);
39+
*response.body_mut() = Body::from(mapping);
4440
}
4541

4642
// Reverse the entire body before sending back to the client.
@@ -49,32 +45,42 @@ fn echo(req: Request<Body>) -> BoxFut {
4945
// the chunks as they arrive. So, this returns a different
5046
// future, waiting on concatenating the full body, so that
5147
// it can be reversed. Only then can we return a `Response`.
52-
(&Method::POST, "/echo/reversed") => {
53-
let reversed = req.into_body().concat2().map(move |chunk| {
54-
let body = chunk.iter().rev().cloned().collect::<Vec<u8>>();
55-
*response.body_mut() = Body::from(body);
56-
response
57-
});
58-
59-
return Box::new(reversed);
60-
}
48+
// (&Method::POST, "/echo/reversed") => {
49+
// let reversed = req.into_body().concat2().map(move |chunk| {
50+
// let body = chunk.iter().rev().cloned().collect::<Vec<u8>>();
51+
// *response.body_mut() = Body::from(body);
52+
// response
53+
// });
54+
//
55+
// return Ok(reversed);
56+
// }
6157

6258
// The 404 Not Found route...
6359
_ => {
6460
*response.status_mut() = StatusCode::NOT_FOUND;
6561
}
6662
};
6763

68-
Box::new(future::ok(response))
64+
Ok(response)
6965
}
7066

71-
fn main() {
67+
68+
#[tokio::main]
69+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
7270
let addr = ([127, 0, 0, 1], 3000).into();
7371

72+
let service = make_service_fn(|_| {
73+
async {
74+
Ok::<_, hyper::Error>(service_fn(echo))
75+
}
76+
});
77+
7478
let server = Server::bind(&addr)
75-
.serve(|| service_fn(echo))
76-
.map_err(|e| eprintln!("server error: {}", e));
79+
.serve(service);
7780

7881
println!("Listening on http://{}", addr);
79-
hyper::rt::run(server);
82+
83+
server.await?;
84+
85+
Ok(())
8086
}

0 commit comments

Comments
 (0)