Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/cortex-rs/blob/main/src/main.rs # cortex-rs Pure compiled native Rust memory engine with vector search and SQLite WAL Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `cortex-rs / src / main.rs` 123 lines ยท 4146 bytes Raw Blame History 1 use axum::{ 2 middleware, 3 routing::{get, post}, 4 Router, 5 }; 6 use clap::Parser; 7 use reqwest::Client; 8 use std::fs; 9 use std::net::SocketAddr; 10 use std::path::PathBuf; 11 use std::sync::Arc; 12 use tower_http::cors::{Any, CorsLayer}; 13 use tower_http::trace::TraceLayer; 14 use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; 15 16 mod auth; 17 mod db; 18 mod embeddings; 19 mod handlers; 20 mod models; 21 22 use auth::auth_middleware; 23 use db::Database; 24 use handlers::*; 25 use models::CortexEntity; 26 27 #[derive(Parser, Debug)] 28 #[command(name = "cortex-rs", about = "Sovereign Native Memory Engine in Rust")] 29 struct Args { 30 #[arg(long, default_value = "127.0.0.1")] 31 host: String, 32 33 #[arg(long, default_value = "18080")] 34 port: u16, 35 36 #[arg(long, default_value = "/home/drakestapleton/.config/cortex/cortex.db")] 37 db_path: PathBuf, 38 39 #[arg(long, default_value = "http://127.0.0.1:18081")] 40 encoder_url: String, 41 42 #[arg(long)] 43 import: Option, 44 } 45 46 #[tokio::main] 47 async fn main() -> Result<(), Box> { 48 let args = Args::parse(); 49 50 tracing_subscriber::registry() 51 .with(tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| "info".into())) 52 .with(tracing_subscriber::fmt::layer()) 53 .init(); 54 55 tracing::info!("Initializing Cortex-RS SQLite database at {:?}...", args.db_path); 56 if let Some(parent) = args.db_path.parent() { 57 let _ = fs::create_dir_all(parent); 58 } 59 60 let database = Arc::new(Database::open(&args.db_path)?); 61 62 if let Some(import_file) = &args.import { 63 tracing::info!("Importing existing entities from {:?}...", import_file); 64 if import_file.exists() { 65 let data = fs::read_to_string(import_file)?; 66 let raw_list: Vec = serde_json::from_str(&data)?; 67 let mut count = 0; 68 for val in raw_list { 69 let entity_val = if let Some(e) = val.get("entity") { e.clone() } else { val }; 70 if let Ok(entity) = serde_json::from_value::(entity_val) { 71 let _ = database.import_entity_raw(&entity, None); 72 count += 1; 73 } 74 } 75 tracing::info!("Successfully imported {} entities into SQLite!", count); 76 } 77 } 78 79 let state = Arc::new(AppState { 80 db: database, 81 http_client: Client::builder().build()?, 82 encoder_url: args.encoder_url, 83 }); 84 85 let protected_routes = Router::new() 86 .route("/api/cortex/write", post(write_handler)) 87 .route("/api/cortex/search", get(search_get_handler).post(search_post_handler)) 88 .route("/api/cortex/recall", post(recall_handler)) 89 .route("/api/cortex/get", get(get_handler)) 90 .route("/api/cortex/traverse", post(traverse_handler)) 91 .layer(middleware::from_fn(auth_middleware)); 92 93 let app = Router::new() 94 .route("/health", get(health_handler)) 95 .route("/status", get(health_handler)) 96 .merge(protected_routes) 97 .layer( 98 CorsLayer::new() 99 .allow_origin([ 100 "http://127.0.0.1:18095".parse().unwrap(), 101 "http://localhost:18095".parse().unwrap(), 102 "http://192.168.1.108:18095".parse().unwrap(), 103 "http://100.116.106.93:18095".parse().unwrap(), 104 ]) 105 .allow_methods(Any) 106 .allow_headers(Any), 107 ) 108 .layer(TraceLayer::new_for_http()) 109 .with_state(state); 110 111 // INVARIANT: Bind strictly to loopback 127.0.0.1 to guarantee zero LAN exposure 112 if args.host != "127.0.0.1" && args.host != "localhost" { 113 tracing::warn!("Non-loopback binding detected ({}); enforcing local authentication.", args.host); 114 } 115 116 let addr: SocketAddr = format!("{}:{}", args.host, args.port).parse().unwrap_or_else(|_| SocketAddr::from(([127, 0, 0, 1], args.port))); 117 tracing::info!("Cortex-RS server listening on http://{}", addr); 118 119 let listener = tokio::net::TcpListener::bind(addr).await?; 120 axum::serve(listener, app).await?; 121 122 Ok(()) 123 }