mirror of
https://github.com/tokio-rs/axum.git
synced 2024-11-24 08:06:36 +01:00
Update example reverse-proxy to axum 0.7 (#2395)
This commit is contained in:
parent
801a78a4cf
commit
b02ce30737
2 changed files with 52 additions and 53 deletions
|
@ -6,4 +6,5 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
axum = { path = "../../axum" }
|
axum = { path = "../../axum" }
|
||||||
hyper = { version = "1.0.0", features = ["full"] }
|
hyper = { version = "1.0.0", features = ["full"] }
|
||||||
|
hyper-util = { version = "0.1.1", features = ["client-legacy"] }
|
||||||
tokio = { version = "1", features = ["full"] }
|
tokio = { version = "1", features = ["full"] }
|
||||||
|
|
|
@ -7,63 +7,61 @@
|
||||||
//! cargo run -p example-reverse-proxy
|
//! cargo run -p example-reverse-proxy
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
// TODO
|
use axum::{
|
||||||
fn main() {
|
body::Body,
|
||||||
eprint!("this example has not yet been updated to hyper 1.0");
|
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<HttpConnector, Body>;
|
||||||
|
|
||||||
|
#[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::{
|
async fn handler(State(client): State<Client>, mut req: Request) -> Result<Response, StatusCode> {
|
||||||
// body::Body,
|
let path = req.uri().path();
|
||||||
// extract::{Request, State},
|
let path_query = req
|
||||||
// http::uri::Uri,
|
.uri()
|
||||||
// response::{IntoResponse, Response},
|
.path_and_query()
|
||||||
// routing::get,
|
.map(|v| v.as_str())
|
||||||
// Router,
|
.unwrap_or(path);
|
||||||
// };
|
|
||||||
// use hyper::{client::HttpConnector, StatusCode};
|
|
||||||
|
|
||||||
// type Client = hyper::client::Client<HttpConnector, Body>;
|
let uri = format!("http://127.0.0.1:3000{}", path_query);
|
||||||
|
|
||||||
// #[tokio::main]
|
*req.uri_mut() = Uri::try_from(uri).unwrap();
|
||||||
// async fn main() {
|
|
||||||
// tokio::spawn(server());
|
|
||||||
|
|
||||||
// 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")
|
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
|
||||||
// .await
|
.await
|
||||||
// .unwrap();
|
.unwrap();
|
||||||
// println!("listening on {}", listener.local_addr().unwrap());
|
println!("listening on {}", listener.local_addr().unwrap());
|
||||||
// axum::serve(listener, app).await.unwrap();
|
axum::serve(listener, app).await.unwrap();
|
||||||
// }
|
}
|
||||||
|
|
||||||
// async fn handler(State(client): State<Client>, mut req: Request) -> Result<Response, StatusCode> {
|
|
||||||
// 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();
|
|
||||||
// }
|
|
||||||
|
|
Loading…
Reference in a new issue