Update to hyper 1.0-rc.3 and http-body-util 0.1.0-rc.2

This commit is contained in:
David Pedersen
2023-03-23 23:23:39 +01:00
parent 44bb38dfa2
commit c73b6a9969
41 changed files with 790 additions and 816 deletions
+49 -44
View File
@@ -7,54 +7,59 @@
//! cargo run -p example-reverse-proxy
//! ```
use axum::{
body::Body,
extract::{Request, State},
http::uri::Uri,
response::{IntoResponse, Response},
routing::get,
Router,
};
use hyper::client::HttpConnector;
type Client = hyper::client::Client<HttpConnector, Body>;
#[tokio::main]
async fn main() {
tokio::spawn(server());
let client: Client = hyper::Client::builder().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();
// TODO
fn main() {
eprint!("this example has not yet been updated to hyper 1.0");
}
async fn handler(State(client): State<Client>, mut req: Request) -> Response {
let path = req.uri().path();
let path_query = req
.uri()
.path_and_query()
.map(|v| v.as_str())
.unwrap_or(path);
// use axum::{
// body::Body,
// extract::{Request, State},
// http::uri::Uri,
// response::{IntoResponse, Response},
// routing::get,
// Router,
// };
// // use hyper::client::HttpConnector;
let uri = format!("http://127.0.0.1:3000{}", path_query);
// // type Client = hyper::client::Client<HttpConnector, Body>;
*req.uri_mut() = Uri::try_from(uri).unwrap();
// #[tokio::main]
// async fn main() {
// tokio::spawn(server());
client.request(req).await.unwrap().into_response()
}
// let client: Client = hyper::Client::builder().build(HttpConnector::new());
async fn server() {
let app = Router::new().route("/", get(|| async { "Hello, world!" }));
// let app = Router::new().route("/", get(handler)).with_state(client);
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:4000")
// .await
// .unwrap();
// println!("listening on {}", listener.local_addr().unwrap());
// axum::serve(listener, app).await.unwrap();
// }
// async fn handler(State(client): State<Client>, mut req: Request) -> Response {
// 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();
// client.request(req).await.unwrap().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();
// }