2021-10-19 22:52:19 +02:00
|
|
|
//! Reverse proxy listening in "localhost:4000" will proxy all requests to "localhost:3000"
|
|
|
|
//! endpoint.
|
|
|
|
//!
|
|
|
|
//! Run with
|
|
|
|
//!
|
|
|
|
//! ```not_rust
|
2023-03-10 12:02:11 +01:00
|
|
|
//! cargo run -p example-reverse-proxy
|
2021-10-19 22:52:19 +02:00
|
|
|
//! ```
|
|
|
|
|
2023-11-23 12:03:03 +01:00
|
|
|
// TODO
|
|
|
|
fn main() {
|
|
|
|
eprint!("this example has not yet been updated to hyper 1.0");
|
|
|
|
}
|
2021-10-19 22:52:19 +02:00
|
|
|
|
2023-11-23 12:03:03 +01:00
|
|
|
// use axum::{
|
|
|
|
// body::Body,
|
|
|
|
// extract::{Request, State},
|
|
|
|
// http::uri::Uri,
|
|
|
|
// response::{IntoResponse, Response},
|
|
|
|
// routing::get,
|
|
|
|
// Router,
|
|
|
|
// };
|
|
|
|
// use hyper::{client::HttpConnector, StatusCode};
|
2021-10-19 22:52:19 +02:00
|
|
|
|
2023-11-23 12:03:03 +01:00
|
|
|
// type Client = hyper::client::Client<HttpConnector, Body>;
|
2021-10-19 22:52:19 +02:00
|
|
|
|
2023-11-23 12:03:03 +01:00
|
|
|
// #[tokio::main]
|
|
|
|
// async fn main() {
|
|
|
|
// tokio::spawn(server());
|
2021-10-19 22:52:19 +02:00
|
|
|
|
2023-11-23 12:03:03 +01:00
|
|
|
// let client: Client = hyper::Client::builder().build(HttpConnector::new());
|
2021-10-19 22:52:19 +02:00
|
|
|
|
2023-11-23 12:03:03 +01:00
|
|
|
// let app = Router::new().route("/", get(handler)).with_state(client);
|
2021-10-19 22:52:19 +02:00
|
|
|
|
2023-11-23 12:03:03 +01:00
|
|
|
// 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();
|
|
|
|
// }
|
2021-10-19 22:52:19 +02:00
|
|
|
|
2023-11-23 12:03:03 +01:00
|
|
|
// 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);
|
2021-10-19 22:52:19 +02:00
|
|
|
|
2023-11-23 12:03:03 +01:00
|
|
|
// let uri = format!("http://127.0.0.1:3000{}", path_query);
|
2021-10-19 22:52:19 +02:00
|
|
|
|
2023-11-23 12:03:03 +01:00
|
|
|
// *req.uri_mut() = Uri::try_from(uri).unwrap();
|
2021-10-19 22:52:19 +02:00
|
|
|
|
2023-11-23 12:03:03 +01:00
|
|
|
// Ok(client
|
|
|
|
// .request(req)
|
|
|
|
// .await
|
|
|
|
// .map_err(|_| StatusCode::BAD_REQUEST)?
|
|
|
|
// .into_response())
|
|
|
|
// }
|
2021-10-19 22:52:19 +02:00
|
|
|
|
2023-11-23 12:03:03 +01:00
|
|
|
// 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();
|
|
|
|
// }
|