Add and prepare rust worker management system for file information processing and knowledge base framework

This commit is contained in:
Christbru 2025-10-19 03:53:02 -05:00
commit da6ab3a782
12 changed files with 1402 additions and 251 deletions

View file

@ -1,21 +1,16 @@
mod api;
mod db;
mod gemini_client;
mod models;
mod storage;
mod vector;
mod worker;
mod vector_db;
use std::env;
use std::error::Error;
use tracing::info;
use warp::Filter;
use sqlx::mysql::MySqlPool;
use serde::{Deserialize, Serialize};
use tracing::{info, warn};
#[derive(Debug, Serialize, Deserialize)]
struct HealthResponse {
status: String,
timestamp: String,
}
#[derive(Debug, Serialize, Deserialize)]
struct ApiResponse<T> {
success: bool,
data: Option<T>,
message: Option<String>,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
@ -29,103 +24,28 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.unwrap_or_else(|_| "mysql://astraadmin:password@mysql:3306/astra".to_string());
info!("Starting Rust Engine...");
// info!("Connecting to database: {}", database_url);
// Connect to database
let pool = match MySqlPool::connect(&database_url).await {
Ok(pool) => {
info!("Successfully connected to database");
pool
}
Err(e) => {
warn!("Failed to connect to database: {}. Starting without DB connection.", e);
// In a hackathon setting, we might want to continue without DB for initial testing
return start_server_without_db().await;
}
};
// Ensure storage dir
storage::ensure_storage_dir().expect("storage dir");
// CORS configuration
let cors = warp::cors()
.allow_any_origin()
.allow_headers(vec!["content-type", "authorization"])
.allow_methods(vec!["GET", "POST", "PUT", "DELETE", "OPTIONS"]);
// Initialize DB
let pool = db::init_db(&database_url).await.map_err(|e| -> Box<dyn Error> { Box::new(e) })?;
// Health check endpoint
let health = warp::path("health")
.and(warp::get())
.map(|| {
let response = HealthResponse {
status: "healthy".to_string(),
timestamp: chrono::Utc::now().to_rfc3339(),
};
warp::reply::json(&ApiResponse {
success: true,
data: Some(response),
message: None,
})
});
// Spawn worker
let worker = worker::Worker::new(pool.clone());
tokio::spawn(async move { worker.run().await });
// API routes - you'll expand these for your hackathon needs
let api = warp::path("api")
.and(
health.or(
// Add more routes here as needed
warp::path("version")
.and(warp::get())
.map(|| {
warp::reply::json(&ApiResponse {
success: true,
data: Some("1.0.0"),
message: Some("Rust Engine API".to_string()),
})
})
)
);
let routes = api
.with(cors)
// API routes
let api_routes = api::routes(pool.clone())
.with(warp::cors()
.allow_any_origin()
.allow_headers(vec!["content-type", "authorization"])
.allow_methods(vec!["GET", "POST", "PUT", "DELETE", "OPTIONS"]))
.with(warp::log("rust_engine"));
info!("Rust Engine started on http://0.0.0.0:8000");
warp::serve(routes)
.run(([0, 0, 0, 0], 8000))
.await;
Ok(())
}
async fn start_server_without_db() -> Result<(), Box<dyn std::error::Error>> {
info!("Starting server in DB-less mode for development");
let cors = warp::cors()
.allow_any_origin()
.allow_headers(vec!["content-type", "authorization"])
.allow_methods(vec!["GET", "POST", "PUT", "DELETE", "OPTIONS"]);
let health = warp::path("health")
.and(warp::get())
.map(|| {
let response = HealthResponse {
status: "healthy (no db)".to_string(),
timestamp: chrono::Utc::now().to_rfc3339(),
};
warp::reply::json(&ApiResponse {
success: true,
data: Some(response),
message: Some("Running without database connection".to_string()),
})
});
let routes = warp::path("api")
.and(health)
.with(cors)
.with(warp::log("rust_engine"));
info!("Rust Engine started on http://0.0.0.0:8000 (DB-less mode)");
info!("Rust Engine prepared!");
warp::serve(routes)
warp::serve(api_routes)
.run(([0, 0, 0, 0], 8000))
.await;