fix: add ConnectInfo extension and error logging to server handlers
Co-authored-by: aider (openai/andrew/openrouter/anthropic/claude-sonnet-4) <aider@aider.chat>
This commit is contained in:
@@ -6,6 +6,7 @@ use axum::{
|
|||||||
routing::get,
|
routing::get,
|
||||||
Router,
|
Router,
|
||||||
};
|
};
|
||||||
|
use axum::extract::connect_info::ConnectInfo as AxumConnectInfo;
|
||||||
use clap::Command;
|
use clap::Command;
|
||||||
use log::{debug, info, warn};
|
use log::{debug, info, warn};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
@@ -19,6 +20,7 @@ use tokio::sync::Mutex;
|
|||||||
use tower_http::cors::CorsLayer;
|
use tower_http::cors::CorsLayer;
|
||||||
use tower::ServiceBuilder;
|
use tower::ServiceBuilder;
|
||||||
use tower_http::trace::TraceLayer;
|
use tower_http::trace::TraceLayer;
|
||||||
|
use std::net::SocketAddr;
|
||||||
|
|
||||||
use crate::db;
|
use crate::db;
|
||||||
use crate::Args;
|
use crate::Args;
|
||||||
@@ -128,6 +130,7 @@ async fn run_server(
|
|||||||
ServiceBuilder::new()
|
ServiceBuilder::new()
|
||||||
.layer(TraceLayer::new_for_http())
|
.layer(TraceLayer::new_for_http())
|
||||||
.layer(CorsLayer::permissive())
|
.layer(CorsLayer::permissive())
|
||||||
|
.layer(axum::extract::connect_info::IntoMakeServiceWithConnectInfo::into_make_service_with_connect_info::<SocketAddr>)
|
||||||
)
|
)
|
||||||
.with_state(state);
|
.with_state(state);
|
||||||
|
|
||||||
@@ -216,10 +219,16 @@ async fn handle_list_items(
|
|||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
let items = if tags.is_empty() {
|
let items = if tags.is_empty() {
|
||||||
db::get_items(&mut *conn).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
|
db::get_items(&mut *conn).map_err(|e| {
|
||||||
|
warn!("Failed to get items: {}", e);
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR
|
||||||
|
})?
|
||||||
} else {
|
} else {
|
||||||
db::get_items_matching(&mut *conn, &tags, &HashMap::new())
|
db::get_items_matching(&mut *conn, &tags, &HashMap::new())
|
||||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
|
.map_err(|e| {
|
||||||
|
warn!("Failed to get items matching tags {:?}: {}", tags, e);
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR
|
||||||
|
})?
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get item IDs for batch queries
|
// Get item IDs for batch queries
|
||||||
@@ -227,9 +236,15 @@ async fn handle_list_items(
|
|||||||
|
|
||||||
// Get tags and metadata for all items
|
// Get tags and metadata for all items
|
||||||
let tags_map = db::get_tags_for_items(&mut *conn, &item_ids)
|
let tags_map = db::get_tags_for_items(&mut *conn, &item_ids)
|
||||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
.map_err(|e| {
|
||||||
|
warn!("Failed to get tags for items: {}", e);
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR
|
||||||
|
})?;
|
||||||
let meta_map = db::get_meta_for_items(&mut *conn, &item_ids)
|
let meta_map = db::get_meta_for_items(&mut *conn, &item_ids)
|
||||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
.map_err(|e| {
|
||||||
|
warn!("Failed to get metadata for items: {}", e);
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR
|
||||||
|
})?;
|
||||||
|
|
||||||
let item_infos: Vec<ItemInfo> = items
|
let item_infos: Vec<ItemInfo> = items
|
||||||
.into_iter()
|
.into_iter()
|
||||||
@@ -278,26 +293,39 @@ async fn handle_get_item(
|
|||||||
let mut conn = state.db.lock().await;
|
let mut conn = state.db.lock().await;
|
||||||
|
|
||||||
let item = if let Ok(id) = item_id.parse::<i64>() {
|
let item = if let Ok(id) = item_id.parse::<i64>() {
|
||||||
db::get_item(&mut *conn, id).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
|
db::get_item(&mut *conn, id).map_err(|e| {
|
||||||
|
warn!("Failed to get item {}: {}", id, e);
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR
|
||||||
|
})?
|
||||||
} else {
|
} else {
|
||||||
// Try to find by tags
|
// Try to find by tags
|
||||||
if let Some(tags_str) = params.tags {
|
if let Some(tags_str) = params.tags {
|
||||||
let tags: Vec<String> = tags_str.split(',').map(|t| t.trim().to_string()).collect();
|
let tags: Vec<String> = tags_str.split(',').map(|t| t.trim().to_string()).collect();
|
||||||
db::get_item_matching(&mut *conn, &tags, &HashMap::new())
|
db::get_item_matching(&mut *conn, &tags, &HashMap::new())
|
||||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
|
.map_err(|e| {
|
||||||
|
warn!("Failed to get item matching tags {:?}: {}", tags, e);
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR
|
||||||
|
})?
|
||||||
} else {
|
} else {
|
||||||
|
warn!("Invalid item ID '{}' and no tags provided", item_id);
|
||||||
return Err(StatusCode::BAD_REQUEST);
|
return Err(StatusCode::BAD_REQUEST);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(item) = item {
|
if let Some(item) = item {
|
||||||
let item_tags = db::get_item_tags(&mut *conn, &item)
|
let item_tags = db::get_item_tags(&mut *conn, &item)
|
||||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
|
.map_err(|e| {
|
||||||
|
warn!("Failed to get tags for item {}: {}", item.id.unwrap_or(0), e);
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR
|
||||||
|
})?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|t| t.name)
|
.map(|t| t.name)
|
||||||
.collect();
|
.collect();
|
||||||
let item_meta = db::get_item_meta(&mut *conn, &item)
|
let item_meta = db::get_item_meta(&mut *conn, &item)
|
||||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
|
.map_err(|e| {
|
||||||
|
warn!("Failed to get metadata for item {}: {}", item.id.unwrap_or(0), e);
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR
|
||||||
|
})?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|m| (m.name, m.value))
|
.map(|m| (m.name, m.value))
|
||||||
.collect();
|
.collect();
|
||||||
@@ -362,8 +390,14 @@ async fn handle_delete_item(
|
|||||||
if let Ok(id) = item_id.parse::<i64>() {
|
if let Ok(id) = item_id.parse::<i64>() {
|
||||||
let mut conn = state.db.lock().await;
|
let mut conn = state.db.lock().await;
|
||||||
|
|
||||||
if let Some(item) = db::get_item(&mut *conn, id).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)? {
|
if let Some(item) = db::get_item(&mut *conn, id).map_err(|e| {
|
||||||
db::delete_item(&mut *conn, item).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
warn!("Failed to get item {} for deletion: {}", id, e);
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR
|
||||||
|
})? {
|
||||||
|
db::delete_item(&mut *conn, item).map_err(|e| {
|
||||||
|
warn!("Failed to delete item {}: {}", id, e);
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR
|
||||||
|
})?;
|
||||||
|
|
||||||
let response = ApiResponse::<()> {
|
let response = ApiResponse::<()> {
|
||||||
success: true,
|
success: true,
|
||||||
@@ -396,9 +430,15 @@ async fn handle_get_content_latest(
|
|||||||
let item = if let Some(tags_str) = params.tags {
|
let item = if let Some(tags_str) = params.tags {
|
||||||
let tags: Vec<String> = tags_str.split(',').map(|t| t.trim().to_string()).collect();
|
let tags: Vec<String> = tags_str.split(',').map(|t| t.trim().to_string()).collect();
|
||||||
db::get_item_matching(&mut *conn, &tags, &HashMap::new())
|
db::get_item_matching(&mut *conn, &tags, &HashMap::new())
|
||||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
|
.map_err(|e| {
|
||||||
|
warn!("Failed to get item matching tags {:?} for content: {}", tags, e);
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR
|
||||||
|
})?
|
||||||
} else {
|
} else {
|
||||||
db::get_item_last(&mut *conn).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
|
db::get_item_last(&mut *conn).map_err(|e| {
|
||||||
|
warn!("Failed to get last item for content: {}", e);
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR
|
||||||
|
})?
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(_item) = item {
|
if let Some(_item) = item {
|
||||||
@@ -430,7 +470,10 @@ async fn handle_get_content(
|
|||||||
if let Ok(id) = item_id.parse::<i64>() {
|
if let Ok(id) = item_id.parse::<i64>() {
|
||||||
let mut conn = state.db.lock().await;
|
let mut conn = state.db.lock().await;
|
||||||
|
|
||||||
if let Some(_item) = db::get_item(&mut *conn, id).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)? {
|
if let Some(_item) = db::get_item(&mut *conn, id).map_err(|e| {
|
||||||
|
warn!("Failed to get item {} for content: {}", id, e);
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR
|
||||||
|
})? {
|
||||||
// Get the actual content - this would need to be implemented
|
// Get the actual content - this would need to be implemented
|
||||||
// based on how content is stored and retrieved in your system
|
// based on how content is stored and retrieved in your system
|
||||||
let response = ApiResponse::<String> {
|
let response = ApiResponse::<String> {
|
||||||
|
|||||||
Reference in New Issue
Block a user