Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/server/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::core::{Database, EmbeddingEngine};
pub struct ServerState {
embedding_engine: Mutex<EmbeddingEngine>,
config: Config,
db: Mutex<Database>,
}

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -98,9 +99,12 @@ pub async fn run_server(config: &Config, host: &str, port: u16) -> Result<()> {
println!(" {}Model loaded successfully!", crate::ui::CHECK);
println!();

let db = Database::new(&config.db_path().unwrap_or_default())?;

let state = Arc::new(ServerState {
embedding_engine: Mutex::new(engine),
config,
db: Mutex::new(db),
});

let cors = CorsLayer::new()
Expand Down Expand Up @@ -143,13 +147,13 @@ async fn health() -> impl IntoResponse {
}

async fn status(State(state): State<SharedState>) -> impl IntoResponse {
let db = match Database::new(&state.config.db_path().unwrap_or_default()) {
let db = match state.db.lock() {
Ok(db) => db,
Err(e) => {
return (
StatusCode::INTERNAL_SERVER_ERROR,
Json(serde_json::json!({
"error": format!("Failed to open database: {}", e)
"error": format!("Failed to lock database: {}", e)
})),
)
.into_response();
Expand Down Expand Up @@ -228,13 +232,13 @@ async fn search(
};

// Search in database
let db = match Database::new(&state.config.db_path().unwrap_or_default()) {
let db = match state.db.lock() {
Ok(db) => db,
Err(e) => {
return (
StatusCode::INTERNAL_SERVER_ERROR,
Json(serde_json::json!({
"error": format!("Failed to open database: {}", e)
"error": format!("Failed to lock database: {}", e)
})),
)
.into_response();
Expand Down
2 changes: 1 addition & 1 deletion src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
mod api;
mod client;

pub use api::run_server;
pub use api::{run_server, ServerState};
pub use client::Client;
Loading