Skip to content

Commit

Permalink
feat: Update reverse proxy example to support both http and https
Browse files Browse the repository at this point in the history
  • Loading branch information
afifurrohman-id committed Apr 2, 2024
1 parent 50c035c commit d29cfd7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/reverse-proxy/Cargo.toml
Expand Up @@ -5,6 +5,6 @@ edition = "2021"

[dependencies]
axum = { path = "../../axum" }
hyper = { version = "1.0.0", features = ["full"] }
hyper-tls = {version = "0.6.0", features = ["vendored"]}
hyper-util = { version = "0.1.1", features = ["client-legacy"] }
tokio = { version = "1", features = ["full"] }
28 changes: 19 additions & 9 deletions examples/reverse-proxy/src/main.rs
@@ -1,6 +1,8 @@
//! Reverse proxy listening in "localhost:4000" will proxy all requests to "localhost:3000"
//! Reverse proxy listening in "localhost:4000" will proxy all `GET` requests to "localhost:3000" except for path /https is example.com
//! endpoint.
//!
//! On unix like OS: make sure `ca-certificates` is installed.
//!
//! Run with
//!
//! ```not_rust
Expand All @@ -12,23 +14,25 @@ use axum::{
extract::{Request, State},
http::uri::Uri,
response::{IntoResponse, Response},
routing::get,
Router,
routing, Router,
};
use hyper::StatusCode;
use axum::http::{header::HOST, StatusCode};
use hyper_tls::HttpsConnector;
use hyper_util::{client::legacy::connect::HttpConnector, rt::TokioExecutor};

type Client = hyper_util::client::legacy::Client<HttpConnector, Body>;
type Client = hyper_util::client::legacy::Client<HttpsConnector<HttpConnector>, Body>;

#[tokio::main]
async fn main() {
tokio::spawn(server());

let client: Client =
hyper_util::client::legacy::Client::<(), ()>::builder(TokioExecutor::new())
.build(HttpConnector::new());
.build(HttpsConnector::new());

let app = Router::new().route("/", get(handler)).with_state(client);
let app = Router::new()
.fallback(routing::get(handler))
.with_state(client);

let listener = tokio::net::TcpListener::bind("127.0.0.1:4000")
.await
Expand All @@ -45,10 +49,16 @@ async fn handler(State(client): State<Client>, mut req: Request) -> Result<Respo
.map(|v| v.as_str())
.unwrap_or(path);

let uri = format!("http://127.0.0.1:3000{}", path_query);
let mut uri = format!("http://127.0.0.1:3000{}", path_query);
if path == "/https" {
uri = format!("https://example.com")
}

*req.uri_mut() = Uri::try_from(uri).unwrap();

//? Remove incorrect header host, hyper will add automatically for you.
req.headers_mut().remove(HOST).unwrap();

Ok(client
.request(req)
.await
Expand All @@ -57,7 +67,7 @@ async fn handler(State(client): State<Client>, mut req: Request) -> Result<Respo
}

async fn server() {
let app = Router::new().route("/", get(|| async { "Hello, world!" }));
let app = Router::new().fallback(routing::get(|| async { "Hello, world!" }));

let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
.await
Expand Down

0 comments on commit d29cfd7

Please sign in to comment.