Something went wrong. Try again.
ceres: a small planet in a giant solar system
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182use axum::{ Json, body::{self, Bytes}, extract::{Request, State}, http::{Method, StatusCode}, response::{IntoResponse, Response},};use log::info;use serde_json::{Value, json};
use crate::state::AppState;
pub async fn log_request(State(state): State<AppState>, req: Request) -> Response { let (parts, body) = req.into_parts(); let method = parts.method.clone(); let path = parts.uri.path().to_string(); let query = parts .uri .query() .map(|q| format!("?{}", q)) .unwrap_or_default();
info!("{method} {path}{query}");
if method == Method::GET { match parts.uri.query() { Some(q) => info!(" query: {q}"), None => info!(" query: <none>"), } }
let bytes = match body::to_bytes(body, usize::MAX).await { Ok(b) => b, Err(e) => { info!(" body: <failed to read: {e}>"); if state.forwarded_app_view.is_none() { return (StatusCode::OK, Json(json!({}))).into_response(); } Bytes::new() } };
if !bytes.is_empty() { match serde_json::from_slice::<Value>(&bytes) { Ok(value) => { let pretty = serde_json::to_string_pretty(&value).unwrap_or_else(|_| value.to_string()); info!(" body (json):\n{pretty}"); } Err(_) => { info!(" body: <non-json, {} bytes>", bytes.len()); } } } else if method != Method::GET { info!(" body: <empty>"); }
if let Some(ref base_url) = state.forwarded_app_view { let target_url = format!("{base_url}{path}{query}"); let proxy_res = state .reqwest_client .request(method, &target_url) .body(bytes) .send() .await;
match proxy_res { Ok(res) => { let status = res.status(); let body_bytes = res.bytes().await.unwrap_or_default(); return (status, body_bytes).into_response(); } Err(e) => { log::error!("Proxy request failed: {e}"); return (StatusCode::INTERNAL_SERVER_ERROR, "Proxy Error").into_response(); } } }
(StatusCode::OK, Json(json!({}))).into_response()}