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

@@ -13,6 +13,7 @@ use tower_http::cors::CorsLayer;
use tower::ServiceBuilder;
use tower_http::trace::TraceLayer;
use crate::config;
use crate::services::item_service::ItemService;
pub mod common;
mod api;
@@ -53,17 +54,21 @@ pub fn mode_server(
password_hash: settings.server_password_hash(),
};
// Create ItemService once
let item_service = ItemService::new(data_path.clone());
// We need to move the connection into the async runtime
let rt = tokio::runtime::Runtime::new()?;
// Take ownership of the connection and move it into the async runtime
let owned_conn = std::mem::replace(conn, rusqlite::Connection::open_in_memory()?);
rt.block_on(run_server(server_config, owned_conn, data_path))
rt.block_on(run_server(server_config, owned_conn, data_path, item_service))
}
async fn run_server(
config: common::ServerConfig,
conn: rusqlite::Connection,
data_dir: PathBuf,
item_service: ItemService,
) -> Result<()> {
// Construct address with port
let bind_address = if let Some(port) = config.port {
@@ -80,6 +85,7 @@ async fn run_server(
let state = AppState {
db: db_conn,
data_dir: data_dir.clone(),
item_service: Arc::new(item_service),
};
// Create MCP router