fix: use Axum 0.7 /*path syntax for API proxy routes
This commit is contained in:
parent
2a2f14cbda
commit
300072a5d3
1 changed files with 11 additions and 23 deletions
|
|
@ -1,16 +1,13 @@
|
||||||
//! Normogen frontend server — a minimal Axum app that:
|
//! Normogen frontend server — a minimal Axum app that:
|
||||||
//!
|
//!
|
||||||
//! 1. Serves the built Vite SPA bundle from `dist/` (static files).
|
//! 1. Reverse-proxies `/api/*` requests to the backend container.
|
||||||
//! 2. Falls back to `index.html` for any unmatched route (SPA history mode).
|
//! 2. Serves the built Vite SPA bundle from `dist/` (static files).
|
||||||
//! 3. Reverse-proxies `/api/*` requests to the backend container.
|
//! 3. Falls back to `index.html` for any unmatched route (SPA history mode).
|
||||||
//!
|
|
||||||
//! Designed to run as a standalone container, independently scalable.
|
|
||||||
//! No business logic, no DB — just static serving + proxying.
|
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
body::Body,
|
body::Body,
|
||||||
extract::{Request, State},
|
extract::{Request, State},
|
||||||
http::{HeaderValue, Method, StatusCode, Uri},
|
http::StatusCode,
|
||||||
response::{IntoResponse, Response},
|
response::{IntoResponse, Response},
|
||||||
routing::any,
|
routing::any,
|
||||||
Router,
|
Router,
|
||||||
|
|
@ -19,7 +16,6 @@ use std::env;
|
||||||
use tower_http::services::{ServeDir, ServeFile};
|
use tower_http::services::{ServeDir, ServeFile};
|
||||||
use tower_http::trace::TraceLayer;
|
use tower_http::trace::TraceLayer;
|
||||||
|
|
||||||
/// Shared state: the backend URL + a reqwest client for proxying.
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct ProxyState {
|
struct ProxyState {
|
||||||
backend_url: String,
|
backend_url: String,
|
||||||
|
|
@ -48,8 +44,9 @@ async fn main() {
|
||||||
let serve_dir = ServeDir::new("dist").fallback(ServeFile::new("dist/index.html"));
|
let serve_dir = ServeDir::new("dist").fallback(ServeFile::new("dist/index.html"));
|
||||||
|
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
// Proxy /api/* to the backend.
|
// Proxy /api and everything under /api/ to the backend.
|
||||||
.route("/api/{*rest}", any(proxy_to_backend))
|
.route("/api", any(proxy_to_backend))
|
||||||
|
.route("/api/*path", any(proxy_to_backend))
|
||||||
.with_state(state)
|
.with_state(state)
|
||||||
// Everything else: static files + SPA fallback.
|
// Everything else: static files + SPA fallback.
|
||||||
.fallback_service(serve_dir)
|
.fallback_service(serve_dir)
|
||||||
|
|
@ -68,32 +65,28 @@ async fn main() {
|
||||||
/// query string. Return the backend's response.
|
/// query string. Return the backend's response.
|
||||||
async fn proxy_to_backend(
|
async fn proxy_to_backend(
|
||||||
State(state): State<ProxyState>,
|
State(state): State<ProxyState>,
|
||||||
mut req: Request,
|
req: Request,
|
||||||
) -> Response {
|
) -> Response {
|
||||||
// Build the target URL: backend_url + original path + query string.
|
|
||||||
let path = req.uri().path();
|
let path = req.uri().path();
|
||||||
let query = req.uri().query().map(|q| format!("?{q}")).unwrap_or_default();
|
let query = req.uri().query().map(|q| format!("?{q}")).unwrap_or_default();
|
||||||
let target_url = format!("{}{}{}", state.backend_url, path, query);
|
let target_url = format!("{}{}{}", state.backend_url, path, query);
|
||||||
|
|
||||||
// Map the axum request into a reqwest request.
|
|
||||||
let method = reqwest::Method::from_bytes(req.method().as_str().as_bytes())
|
let method = reqwest::Method::from_bytes(req.method().as_str().as_bytes())
|
||||||
.unwrap_or(reqwest::Method::GET);
|
.unwrap_or(reqwest::Method::GET);
|
||||||
|
|
||||||
// Collect headers (skip host — reqwest sets its own).
|
|
||||||
let mut headers = reqwest::header::HeaderMap::new();
|
let mut headers = reqwest::header::HeaderMap::new();
|
||||||
for (name, value) in req.headers().iter() {
|
for (name, value) in req.headers().iter() {
|
||||||
if name == "host" {
|
if name == "host" {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if let (Ok(name), Ok(value)) = (
|
if let (Ok(n), Ok(v)) = (
|
||||||
reqwest::header::HeaderName::from_bytes(name.as_str().as_bytes()),
|
reqwest::header::HeaderName::from_bytes(name.as_str().as_bytes()),
|
||||||
reqwest::header::HeaderValue::from_bytes(value.as_bytes()),
|
reqwest::header::HeaderValue::from_bytes(value.as_bytes()),
|
||||||
) {
|
) {
|
||||||
headers.insert(name, value);
|
headers.insert(n, v);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Collect the body.
|
|
||||||
let body_bytes = axum::body::to_bytes(req.into_body(), usize::MAX)
|
let body_bytes = axum::body::to_bytes(req.into_body(), usize::MAX)
|
||||||
.await
|
.await
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
@ -108,15 +101,10 @@ async fn proxy_to_backend(
|
||||||
Ok(r) => r,
|
Ok(r) => r,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("Proxy error to {target_url}: {e}");
|
eprintln!("Proxy error to {target_url}: {e}");
|
||||||
return (
|
return (StatusCode::BAD_GATEWAY, "Failed to reach backend").into_response();
|
||||||
StatusCode::BAD_GATEWAY,
|
|
||||||
"Failed to reach backend",
|
|
||||||
)
|
|
||||||
.into_response();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Map the reqwest response back into an axum response.
|
|
||||||
let status = StatusCode::from_u16(backend_resp.status().as_u16())
|
let status = StatusCode::from_u16(backend_resp.status().as_u16())
|
||||||
.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
|
.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue