use axum::{
extract::{Request, State},
http::StatusCode,
response::{IntoResponse, Response},
};
use crate::error::AppError;
use crate::state::AppState;
use crate::proxy;
use crate::middleware::subdomain::Subdomain;
pub async fn subdomain_router(
State(state): State<AppState>,
request: Request,
) -> Result<Response, AppError> {
let subdomain = request
.extensions()
.get::<Subdomain>()
.map(|s| s.0.clone())
.unwrap_or_else(|| "infra".to_string());
if subdomain == "infra" {
Ok(StatusCode::NOT_FOUND.into_response())
} else {
route_user_service(State(state), request).await
}
}
pub async fn route_user_service(
State(state): State<AppState>,
request: Request,
) -> Result<Response, AppError> {
let subdomain = request
.extensions()
.get::<Subdomain>()
.ok_or_else(|| AppError::BadRequest("Missing subdomain".to_string()))?;
let backend_url = state
.config
.routing
.user_routes
.get(&subdomain.0)
.ok_or_else(|| {
AppError::NotFound(format!("No route configured for subdomain: {}", subdomain.0))
})?;
proxy::client::proxy_request(request, backend_url, proxy::client::ProxyConfig::default()).await
}