cleaner code

This commit is contained in:
centdix
2025-05-29 17:54:33 +02:00
parent 5d14c8a28b
commit 0217a41ffa
2 changed files with 23 additions and 67 deletions
+22 -66
View File
@@ -1,9 +1,5 @@
use axum::{
body::Bytes,
response::{IntoResponse, Response},
routing::post,
Router,
};
use crate::db::ApiAuthed;
use axum::{body::Bytes, response::IntoResponse, routing::post, Router};
use http::{HeaderMap, StatusCode};
use reqwest::Client;
use tracing::{error, info, warn};
@@ -21,7 +17,11 @@ pub fn global_service() -> Router {
Router::new().route("/", post(send_inkeep_request))
}
pub async fn send_inkeep_request(headers: HeaderMap, body: Bytes) -> impl IntoResponse {
pub async fn send_inkeep_request(
authed: ApiAuthed,
headers: HeaderMap,
body: Bytes,
) -> impl IntoResponse {
let request_url = format!("{}/inkeep", WINDMILL_CUSTOMER_SERVICE_BASE_URL.as_str());
// Ensure Content-Type is set to application/json
@@ -40,77 +40,33 @@ pub async fn send_inkeep_request(headers: HeaderMap, body: Bytes) -> impl IntoRe
{
Ok(response) => {
let status = response.status();
let response_headers = response.headers().clone();
let mut response_headers = response.headers().clone();
match response.bytes().await {
Ok(response_body) => {
info!("Response body size: {} bytes", response_body.len());
// Remove problematic headers that axum should handle
response_headers.remove("transfer-encoding");
response_headers.remove("content-length");
// Build proper axum Response
let mut response_builder = Response::builder().status(status);
// Filter out hop-by-hop headers that shouldn't be forwarded
let skip_headers = [
"connection",
"keep-alive",
"proxy-authenticate",
"proxy-authorization",
"te",
"trailers",
"transfer-encoding",
"upgrade",
"content-length", // Let axum handle this
];
// Add safe headers from the upstream response
for (key, value) in response_headers.iter() {
let key_str = key.as_str().to_lowercase();
if !skip_headers.contains(&key_str.as_str()) {
response_builder = response_builder.header(key, value);
}
}
// Ensure we have content-type for JSON responses
response_builder = response_builder.header("content-type", "application/json");
match response_builder.body(axum::body::Body::from(response_body)) {
Ok(response) => {
info!("Successfully built response");
response
}
Err(e) => {
error!("Failed to build response: {}", e);
Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.header("content-type", "application/json")
.body(axum::body::Body::from(
r#"{"error": "Failed to build response"}"#,
))
.unwrap()
}
}
(status, response_headers, response_body)
}
Err(e) => {
error!("Failed to read response body: {}", e);
Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.header("content-type", "application/json")
.body(axum::body::Body::from(
r#"{"error": "Failed to read response body"}"#,
))
.unwrap()
(
StatusCode::INTERNAL_SERVER_ERROR,
HeaderMap::new(),
Bytes::from(r#"{"error": "Failed to read response body"}"#),
)
}
}
}
Err(e) => {
error!("Failed to send request to inkeep service: {}", e);
Response::builder()
.status(StatusCode::BAD_GATEWAY)
.header("content-type", "application/json")
.body(axum::body::Body::from(
r#"{"error": "Failed to connect to inkeep service"}"#,
))
.unwrap()
(
StatusCode::BAD_GATEWAY,
HeaderMap::new(),
Bytes::from(r#"{"error": "Failed to connect to inkeep service"}"#),
)
}
}
}
+1 -1
View File
@@ -580,9 +580,9 @@ pub async fn run_server(
.nest("/schedules", schedule::global_service())
.nest("/embeddings", embeddings::global_service())
.nest("/ai", ai::global_service())
.nest("/inkeep", inkeep::global_service())
.route_layer(from_extractor::<ApiAuthed>())
.route_layer(from_extractor::<users::Tokened>())
.nest("/inkeep", inkeep::global_service())
.nest("/jobs", jobs::global_root_service())
.nest(
"/srch/w/:workspace_id/index",