Skip to content

Commit 9519f43

Browse files
committed
switch to proxy pkg
1 parent b73653a commit 9519f43

File tree

3 files changed

+12
-79
lines changed

3 files changed

+12
-79
lines changed

crates/funcd/Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ path = "src/main.rs"
1111

1212
[dependencies]
1313
anyhow = "1.0"
14-
axum = "0.8.6"
14+
axum = { version = "0.8.6", features = ["ws"] }
15+
axum-reverse-proxy = "1.0.3"
1516
http-body-util = "0.1"
16-
hyper = { version = "1.6.1", features = ["client", "http1", "http2"] }
17-
hyper-util = { version = "0.1", features = ["client-legacy", "http1", "http2", "tokio"] }
1817
serde = { version = "1.0.228", features = ["derive"] }
1918
serde_json = "1.0.145"
2019
tokio = { version = "1", features = ["full"] }

crates/funcd/src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ async fn main() -> Result<()> {
6161
};
6262

6363
info!("will proxy requests to port: {}", http_port);
64-
65-
server::serve(&http_addr, http_port).await?;
64+
server::proxy(&http_addr, http_port).await?;
6665
Ok(())
6766
}

crates/funcd/src/server.rs

Lines changed: 9 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,14 @@
11
use anyhow::Result;
2-
use axum::{
3-
Router,
4-
body::Body,
5-
extract::State,
6-
http::{Request, StatusCode},
7-
response::{IntoResponse, Response},
8-
};
9-
use hyper_util::{
10-
// https://github.com/hyperium/hyper/issues/3948
11-
client::legacy::{Client, connect::HttpConnector},
12-
rt::TokioExecutor,
13-
};
14-
use std::sync::Arc;
15-
use tracing::{error, info};
16-
17-
#[derive(Clone)]
18-
struct ProxyState {
19-
client: Client<HttpConnector, Body>,
20-
proxy_port: u16,
21-
}
22-
23-
pub async fn serve(addr: &str, proxy_port: u16) -> Result<()> {
24-
let client = Client::builder(TokioExecutor::new()).build_http();
25-
let state = Arc::new(ProxyState { client, proxy_port });
26-
let router = Router::new().fallback(proxy_handler).with_state(state);
2+
use axum::Router;
3+
use axum_reverse_proxy::ReverseProxy;
4+
use tracing::info;
5+
6+
pub async fn proxy(addr: &str, proxy_port: u16) -> Result<()> {
7+
let process_host = format!("http://localhost:{}", proxy_port);
8+
let proxy = ReverseProxy::new("/", &process_host);
9+
let app: Router = proxy.into();
2710
let listener = tokio::net::TcpListener::bind(&addr).await?;
2811
info!(component = "http", addr = %addr, proxy_port, "listening");
29-
axum::serve(listener, router).await?;
12+
axum::serve(listener, app).await?;
3013
Ok(())
3114
}
32-
33-
async fn proxy_handler(
34-
State(state): State<Arc<ProxyState>>,
35-
mut req: Request<Body>,
36-
) -> Result<Response, RoutingError> {
37-
let proxy_uri = format!(
38-
"http://127.0.0.1:{}{}",
39-
state.proxy_port,
40-
req.uri()
41-
.path_and_query()
42-
.map(|pq| pq.as_str())
43-
.unwrap_or("/")
44-
);
45-
46-
info!(
47-
method = %req.method(),
48-
uri = %req.uri(),
49-
proxy_uri = %proxy_uri,
50-
"proxying request"
51-
);
52-
53-
*req.uri_mut() = proxy_uri.parse().map_err(|e| {
54-
error!("failed to parse proxy URI: {}", e);
55-
RoutingError::BadRequest
56-
})?;
57-
58-
let response = state.client.request(req).await.map_err(|e| {
59-
error!("proxy request failed: {}", e);
60-
RoutingError::ProxyError
61-
})?;
62-
63-
Ok(response.map(Body::new))
64-
}
65-
66-
enum RoutingError {
67-
BadRequest,
68-
ProxyError,
69-
}
70-
71-
impl IntoResponse for RoutingError {
72-
fn into_response(self) -> Response {
73-
let (status, message) = match self {
74-
RoutingError::BadRequest => (StatusCode::BAD_REQUEST, "Bad request"),
75-
RoutingError::ProxyError => (StatusCode::BAD_GATEWAY, "Proxy error"),
76-
};
77-
(status, message).into_response()
78-
}
79-
}

0 commit comments

Comments
 (0)