Skip to content

Commit 1db30e7

Browse files
committed
chore(deps): update exmaples with hyper v1.0
1 parent 267dc9d commit 1db30e7

File tree

2 files changed

+59
-49
lines changed

2 files changed

+59
-49
lines changed

Cargo.toml

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ include = [
2424
smallvec = "1.11.0"
2525

2626
[dev-dependencies]
27+
bytes = "1"
2728
actix-router = "0.5"
2829
ntex-router = "0.5"
2930
path-table = "1.0"
@@ -33,8 +34,10 @@ gonzales = "0.0.3-beta"
3334
futures = "0.3"
3435
rand = "0.8"
3536
criterion = { version = "0.5", features = ["html_reports"] }
36-
hyper = { version="0.14", features = ["full"] }
37-
tokio = { version = "1", default-features = false, features = ["macros", "rt-multi-thread"] }
37+
hyper = { version = "1", features = ["full"] }
38+
hyper-util = { version = "0.1", features = ["tokio"] }
39+
http-body-util = "0.1"
40+
tokio = { version = "1", default-features = false, features = ["macros", "rt-multi-thread", "net"] }
3841

3942
[[bench]]
4043
name = "bench"

examples/hello.rs

+54-47
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,47 @@
11
#![allow(unused_must_use)]
22

3-
use std::{convert::Infallible, future::Future, pin::Pin, sync::Arc};
3+
use std::{convert::Infallible, future::Future, net::SocketAddr, pin::Pin, sync::Arc};
44

5+
use bytes::Bytes;
6+
use http_body_util::Full;
57
use hyper::{
6-
server::Server,
7-
service::{make_service_fn, service_fn},
8-
Body, Request, Response, StatusCode,
8+
body::Incoming, server::conn::http1, service::service_fn, Request, Response, StatusCode,
99
};
10+
use hyper_util::rt::TokioIo;
1011
use path_tree::PathTree;
12+
use tokio::net::TcpListener;
1113

1214
static NOT_FOUND: &[u8] = b"Not Found";
1315

1416
type Params = Vec<(String, String)>;
17+
type Body = Full<Bytes>;
1518

1619
trait Handler: Send + Sync + 'static {
1720
fn call<'a>(
1821
&'a self,
19-
req: Request<Body>,
22+
req: Request<Incoming>,
2023
) -> Pin<Box<dyn Future<Output = Response<Body>> + Send + 'a>>;
2124
}
2225

2326
impl<F, R> Handler for F
2427
where
25-
F: Send + Sync + 'static + Fn(Request<Body>) -> R,
26-
R: Future<Output = Response<Body>> + Send + 'static,
28+
F: Send + Sync + 'static + Fn(Request<Incoming>) -> R,
29+
R: Future<Output = Response<Full<Bytes>>> + Send + 'static,
2730
{
2831
fn call<'a>(
2932
&'a self,
30-
req: Request<Body>,
33+
req: Request<Incoming>,
3134
) -> Pin<Box<dyn Future<Output = Response<Body>> + Send + 'a>> {
3235
let fut = (self)(req);
3336
Box::pin(async move { fut.await })
3437
}
3538
}
3639

37-
async fn index(_: Request<Body>) -> Response<Body> {
40+
async fn index(_: Request<Incoming>) -> Response<Body> {
3841
Response::new(Body::from("Hello, Web!"))
3942
}
4043

41-
async fn hello_world(req: Request<Body>) -> Response<Body> {
44+
async fn hello_world(req: Request<Incoming>) -> Response<Body> {
4245
let params = req.extensions().get::<Params>().unwrap();
4346
let mut s = String::new();
4447
s.push_str("Hello, World!\n");
@@ -48,7 +51,7 @@ async fn hello_world(req: Request<Body>) -> Response<Body> {
4851
Response::new(Body::from(s))
4952
}
5053

51-
async fn hello_user(req: Request<Body>) -> Response<Body> {
54+
async fn hello_user(req: Request<Incoming>) -> Response<Body> {
5255
let params = req.extensions().get::<Params>().unwrap();
5356
let mut s = String::new();
5457
s.push_str("Hello, ");
@@ -59,17 +62,19 @@ async fn hello_user(req: Request<Body>) -> Response<Body> {
5962
Response::new(Body::from(s))
6063
}
6164

62-
async fn hello_rust(_: Request<Body>) -> Response<Body> {
65+
async fn hello_rust(_: Request<Incoming>) -> Response<Body> {
6366
Response::new(Body::from("Hello, Rust!"))
6467
}
6568

66-
async fn login(_req: Request<Body>) -> Response<Body> {
69+
async fn login(_req: Request<Incoming>) -> Response<Body> {
6770
Response::new(Body::from("I'm logined!"))
6871
}
6972

7073
#[tokio::main]
7174
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
72-
let addr = ([127, 0, 0, 1], 3000).into();
75+
let addr: SocketAddr = ([127, 0, 0, 1], 3000).into();
76+
77+
let listener = TcpListener::bind(addr).await?;
7378

7479
// /
7580
// ├── GET/ •0
@@ -87,40 +92,42 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
8792

8893
let tree = Arc::new(tree);
8994

90-
let make_service = make_service_fn(move |_| {
95+
loop {
96+
let (tcp, _) = listener.accept().await?;
97+
let io = TokioIo::new(tcp);
9198
let router = Arc::clone(&tree);
9299

93-
async move {
94-
Ok::<_, Infallible>(service_fn(move |mut req| {
95-
let router = router.clone();
96-
let path = "/".to_owned() + req.method().as_str() + req.uri().path();
97-
98-
async move {
99-
Ok::<_, Infallible>(match router.find(&path) {
100-
Some((handler, route)) => {
101-
let p = route
102-
.params()
103-
.iter()
104-
.map(|p| (p.0.to_string(), p.1.to_string()))
105-
.collect::<Params>();
106-
req.extensions_mut().insert(p);
107-
handler.call(req).await
100+
tokio::task::spawn(async move {
101+
if let Err(err) = http1::Builder::new()
102+
.serve_connection(
103+
io,
104+
service_fn(move |mut req| {
105+
let router = router.clone();
106+
let path = "/".to_owned() + req.method().as_str() + req.uri().path();
107+
108+
async move {
109+
Ok::<_, Infallible>(match router.find(&path) {
110+
Some((handler, route)) => {
111+
let p = route
112+
.params()
113+
.iter()
114+
.map(|p| (p.0.to_string(), p.1.to_string()))
115+
.collect::<Params>();
116+
req.extensions_mut().insert(p);
117+
handler.call(req).await
118+
}
119+
None => Response::builder()
120+
.status(StatusCode::NOT_FOUND)
121+
.body(NOT_FOUND.into())
122+
.unwrap(),
123+
})
108124
}
109-
None => Response::builder()
110-
.status(StatusCode::NOT_FOUND)
111-
.body(NOT_FOUND.into())
112-
.unwrap(),
113-
})
114-
}
115-
}))
116-
}
117-
});
118-
119-
let server = Server::bind(&addr).serve(make_service);
120-
121-
println!("Listening on http://{addr}");
122-
123-
server.await?;
124-
125-
Ok(())
125+
}),
126+
)
127+
.await
128+
{
129+
println!("Error serving connection: {:?}", err);
130+
}
131+
});
132+
}
126133
}

0 commit comments

Comments
 (0)