Skip to content

Commit b02ce30

Browse files
authored
Update example reverse-proxy to axum 0.7 (#2395)
1 parent 801a78a commit b02ce30

File tree

2 files changed

+48
-49
lines changed

2 files changed

+48
-49
lines changed

examples/reverse-proxy/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ edition = "2021"
66
[dependencies]
77
axum = { path = "../../axum" }
88
hyper = { version = "1.0.0", features = ["full"] }
9+
hyper-util = { version = "0.1.1", features = ["client-legacy"] }
910
tokio = { version = "1", features = ["full"] }

examples/reverse-proxy/src/main.rs

Lines changed: 47 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,63 +7,61 @@
77
//! cargo run -p example-reverse-proxy
88
//! ```
99
10-
// TODO
11-
fn main() {
12-
eprint!("this example has not yet been updated to hyper 1.0");
13-
}
14-
15-
// use axum::{
16-
// body::Body,
17-
// extract::{Request, State},
18-
// http::uri::Uri,
19-
// response::{IntoResponse, Response},
20-
// routing::get,
21-
// Router,
22-
// };
23-
// use hyper::{client::HttpConnector, StatusCode};
10+
use axum::{
11+
body::Body,
12+
extract::{Request, State},
13+
http::uri::Uri,
14+
response::{IntoResponse, Response},
15+
routing::get,
16+
Router,
17+
};
18+
use hyper::StatusCode;
19+
use hyper_util::{client::legacy::connect::HttpConnector, rt::TokioExecutor};
2420

25-
// type Client = hyper::client::Client<HttpConnector, Body>;
21+
type Client = hyper_util::client::legacy::Client<HttpConnector, Body>;
2622

27-
// #[tokio::main]
28-
// async fn main() {
29-
// tokio::spawn(server());
23+
#[tokio::main]
24+
async fn main() {
25+
tokio::spawn(server());
3026

31-
// let client: Client = hyper::Client::builder().build(HttpConnector::new());
27+
let client: Client =
28+
hyper_util::client::legacy::Client::<(), ()>::builder(TokioExecutor::new())
29+
.build(HttpConnector::new());
3230

33-
// let app = Router::new().route("/", get(handler)).with_state(client);
31+
let app = Router::new().route("/", get(handler)).with_state(client);
3432

35-
// let listener = tokio::net::TcpListener::bind("127.0.0.1:4000")
36-
// .await
37-
// .unwrap();
38-
// println!("listening on {}", listener.local_addr().unwrap());
39-
// axum::serve(listener, app).await.unwrap();
40-
// }
33+
let listener = tokio::net::TcpListener::bind("127.0.0.1:4000")
34+
.await
35+
.unwrap();
36+
println!("listening on {}", listener.local_addr().unwrap());
37+
axum::serve(listener, app).await.unwrap();
38+
}
4139

42-
// async fn handler(State(client): State<Client>, mut req: Request) -> Result<Response, StatusCode> {
43-
// let path = req.uri().path();
44-
// let path_query = req
45-
// .uri()
46-
// .path_and_query()
47-
// .map(|v| v.as_str())
48-
// .unwrap_or(path);
40+
async fn handler(State(client): State<Client>, mut req: Request) -> Result<Response, StatusCode> {
41+
let path = req.uri().path();
42+
let path_query = req
43+
.uri()
44+
.path_and_query()
45+
.map(|v| v.as_str())
46+
.unwrap_or(path);
4947

50-
// let uri = format!("http://127.0.0.1:3000{}", path_query);
48+
let uri = format!("http://127.0.0.1:3000{}", path_query);
5149

52-
// *req.uri_mut() = Uri::try_from(uri).unwrap();
50+
*req.uri_mut() = Uri::try_from(uri).unwrap();
5351

54-
// Ok(client
55-
// .request(req)
56-
// .await
57-
// .map_err(|_| StatusCode::BAD_REQUEST)?
58-
// .into_response())
59-
// }
52+
Ok(client
53+
.request(req)
54+
.await
55+
.map_err(|_| StatusCode::BAD_REQUEST)?
56+
.into_response())
57+
}
6058

61-
// async fn server() {
62-
// let app = Router::new().route("/", get(|| async { "Hello, world!" }));
59+
async fn server() {
60+
let app = Router::new().route("/", get(|| async { "Hello, world!" }));
6361

64-
// let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
65-
// .await
66-
// .unwrap();
67-
// println!("listening on {}", listener.local_addr().unwrap());
68-
// axum::serve(listener, app).await.unwrap();
69-
// }
62+
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
63+
.await
64+
.unwrap();
65+
println!("listening on {}", listener.local_addr().unwrap());
66+
axum::serve(listener, app).await.unwrap();
67+
}

0 commit comments

Comments
 (0)