From b02ce307371a973039018a13fa012af14775948c Mon Sep 17 00:00:00 2001 From: Mape <45532081+markussilvan@users.noreply.github.com> Date: Sat, 2 Dec 2023 12:48:10 +0200 Subject: [PATCH] Update example reverse-proxy to axum 0.7 (#2395) --- examples/reverse-proxy/Cargo.toml | 1 + examples/reverse-proxy/src/main.rs | 104 ++++++++++++++--------------- 2 files changed, 52 insertions(+), 53 deletions(-) diff --git a/examples/reverse-proxy/Cargo.toml b/examples/reverse-proxy/Cargo.toml index 39e8d766..e2816573 100644 --- a/examples/reverse-proxy/Cargo.toml +++ b/examples/reverse-proxy/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" [dependencies] axum = { path = "../../axum" } hyper = { version = "1.0.0", features = ["full"] } +hyper-util = { version = "0.1.1", features = ["client-legacy"] } tokio = { version = "1", features = ["full"] } diff --git a/examples/reverse-proxy/src/main.rs b/examples/reverse-proxy/src/main.rs index d00277e9..db391246 100644 --- a/examples/reverse-proxy/src/main.rs +++ b/examples/reverse-proxy/src/main.rs @@ -7,63 +7,61 @@ //! cargo run -p example-reverse-proxy //! ``` -// TODO -fn main() { - eprint!("this example has not yet been updated to hyper 1.0"); +use axum::{ + body::Body, + extract::{Request, State}, + http::uri::Uri, + response::{IntoResponse, Response}, + routing::get, + Router, +}; +use hyper::StatusCode; +use hyper_util::{client::legacy::connect::HttpConnector, rt::TokioExecutor}; + +type Client = hyper_util::client::legacy::Client; + +#[tokio::main] +async fn main() { + tokio::spawn(server()); + + let client: Client = + hyper_util::client::legacy::Client::<(), ()>::builder(TokioExecutor::new()) + .build(HttpConnector::new()); + + let app = Router::new().route("/", get(handler)).with_state(client); + + let listener = tokio::net::TcpListener::bind("127.0.0.1:4000") + .await + .unwrap(); + println!("listening on {}", listener.local_addr().unwrap()); + axum::serve(listener, app).await.unwrap(); } -// use axum::{ -// body::Body, -// extract::{Request, State}, -// http::uri::Uri, -// response::{IntoResponse, Response}, -// routing::get, -// Router, -// }; -// use hyper::{client::HttpConnector, StatusCode}; +async fn handler(State(client): State, mut req: Request) -> Result { + let path = req.uri().path(); + let path_query = req + .uri() + .path_and_query() + .map(|v| v.as_str()) + .unwrap_or(path); -// type Client = hyper::client::Client; + let uri = format!("http://127.0.0.1:3000{}", path_query); -// #[tokio::main] -// async fn main() { -// tokio::spawn(server()); + *req.uri_mut() = Uri::try_from(uri).unwrap(); -// let client: Client = hyper::Client::builder().build(HttpConnector::new()); + Ok(client + .request(req) + .await + .map_err(|_| StatusCode::BAD_REQUEST)? + .into_response()) +} -// let app = Router::new().route("/", get(handler)).with_state(client); +async fn server() { + let app = Router::new().route("/", get(|| async { "Hello, world!" })); -// let listener = tokio::net::TcpListener::bind("127.0.0.1:4000") -// .await -// .unwrap(); -// println!("listening on {}", listener.local_addr().unwrap()); -// axum::serve(listener, app).await.unwrap(); -// } - -// async fn handler(State(client): State, mut req: Request) -> Result { -// let path = req.uri().path(); -// let path_query = req -// .uri() -// .path_and_query() -// .map(|v| v.as_str()) -// .unwrap_or(path); - -// let uri = format!("http://127.0.0.1:3000{}", path_query); - -// *req.uri_mut() = Uri::try_from(uri).unwrap(); - -// Ok(client -// .request(req) -// .await -// .map_err(|_| StatusCode::BAD_REQUEST)? -// .into_response()) -// } - -// async fn server() { -// let app = Router::new().route("/", get(|| async { "Hello, world!" })); - -// let listener = tokio::net::TcpListener::bind("127.0.0.1:3000") -// .await -// .unwrap(); -// println!("listening on {}", listener.local_addr().unwrap()); -// axum::serve(listener, app).await.unwrap(); -// } + let listener = tokio::net::TcpListener::bind("127.0.0.1:3000") + .await + .unwrap(); + println!("listening on {}", listener.local_addr().unwrap()); + axum::serve(listener, app).await.unwrap(); +}