refactor: optimize item service creation by creating it once per connection

Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-25 20:27:21 -03:00
parent 7eefb64d15
commit 1f11351d9b
4 changed files with 24 additions and 22 deletions

View File

@@ -43,7 +43,7 @@ pub async fn handle_list_items(
.map(|s| s.split(',').map(|t| t.trim().to_string()).collect())
.unwrap_or_default();
let item_service = AsyncItemService::new(state.data_dir.clone(), state.db.clone());
let item_service = AsyncItemService::new(state.data_dir.clone(), state.db.clone(), state.item_service.clone());
let mut items_with_meta = item_service
.list_items(tags, HashMap::new())
.await
@@ -166,7 +166,7 @@ pub async fn handle_get_item_latest_content(
.map(|s| s.split(',').map(|t| t.trim().to_string()).collect())
.unwrap_or_default();
let item_service = AsyncItemService::new(state.data_dir.clone(), state.db.clone());
let item_service = AsyncItemService::new(state.data_dir.clone(), state.db.clone(), state.item_service.clone());
// First get the item metadata to check if it's binary and get MIME type
let item_with_meta = item_service
@@ -220,7 +220,7 @@ pub async fn handle_get_item_content(
return Err(StatusCode::BAD_REQUEST);
}
let item_service = AsyncItemService::new(state.data_dir.clone(), state.db.clone());
let item_service = AsyncItemService::new(state.data_dir.clone(), state.db.clone(), state.item_service.clone());
stream_item_content_response(&item_service, item_id, params.allow_binary, params.offset, params.length).await
}
@@ -314,7 +314,7 @@ pub async fn handle_get_item_latest_meta(
.map(|s| s.split(',').map(|t| t.trim().to_string()).collect())
.unwrap_or_default();
let item_service = AsyncItemService::new(state.data_dir.clone(), state.db.clone());
let item_service = AsyncItemService::new(state.data_dir.clone(), state.db.clone(), state.item_service.clone());
match item_service.find_item(vec![], tags, HashMap::new()).await {
Ok(item_with_meta) => {
@@ -361,7 +361,7 @@ pub async fn handle_get_item_meta(
State(state): State<AppState>,
Path(item_id): Path<i64>,
) -> Result<Json<ApiResponse<HashMap<String, String>>>, StatusCode> {
let item_service = AsyncItemService::new(state.data_dir.clone(), state.db.clone());
let item_service = AsyncItemService::new(state.data_dir.clone(), state.db.clone(), state.item_service.clone());
match item_service.get_item(item_id).await {
Ok(item_with_meta) => {