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/aien-sovereign-core/blob/main/crates/spark-mail-rs/src/api.rs # aien-sovereign-core Autonomous sovereign intelligence engine and runtime core for NVIDIA DGX Spark 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 `aien-sovereign-core / crates / spark-mail-rs / src / api.rs` 145 lines ยท 4401 bytes Raw Blame History 1 use crate::cortex_sync::CortexSync; 2 use crate::models::{EmailMessage, MailboxStatus, SendEmailRequest}; 3 use crate::relay::MailRelay; 4 use crate::store::MailStore; 5 use axum::{ 6 extract::{Path as AxumPath, State}, 7 http::StatusCode, 8 response::Json, 9 routing::{get, post}, 10 Router, 11 }; 12 use serde_json::{json, Value}; 13 use std::sync::Arc; 14 use tower_http::cors::{Any, CorsLayer}; 15 16 #[derive(Clone)] 17 pub struct ApiState { 18 pub store: Arc, 19 pub cortex: Arc, 20 pub relay: Arc, 21 pub smtp_port: u16, 22 pub api_port: u16, 23 pub operator_email: String, 24 } 25 26 pub fn create_router(state: ApiState) -> Router { 27 Router::new() 28 .route("/health", get(handle_health)) 29 .route("/api/mail/status", get(handle_status)) 30 .route("/api/mail/inbox", get(handle_list_inbox)) 31 .route("/api/mail/sent", get(handle_list_sent)) 32 .route("/api/mail/archive", get(handle_list_archive)) 33 .route("/api/mail/message/{id}", get(handle_get_message)) 34 .route("/api/mail/send", post(handle_send_mail)) 35 .route("/api/mail/ingest-test", post(handle_ingest_test)) 36 .layer( 37 CorsLayer::new() 38 .allow_origin(Any) 39 .allow_methods(Any) 40 .allow_headers(Any), 41 ) 42 .with_state(state) 43 } 44 45 async fn handle_health() -> Json { 46 Json(json!({ 47 "status": "ok", 48 "service": "spark-mail-rs", 49 "version": "0.1.0" 50 })) 51 } 52 53 async fn handle_status(State(state): State) -> Json { 54 let cortex_ok = state.cortex.check_health().await; 55 let bridge_ok = state.relay.check_bridge().await; 56 57 let status = state.store.get_status( 58 &state.operator_email, 59 state.smtp_port, 60 state.api_port, 61 cortex_ok, 62 bridge_ok, 63 ); 64 Json(status) 65 } 66 67 async fn handle_list_inbox(State(state): State) -> Json> { 68 let messages = state.store.list_folder("inbox"); 69 Json(messages) 70 } 71 72 async fn handle_list_sent(State(state): State) -> Json> { 73 let messages = state.store.list_folder("sent"); 74 Json(messages) 75 } 76 77 async fn handle_list_archive(State(state): State) -> Json> { 78 let messages = state.store.list_folder("archive"); 79 Json(messages) 80 } 81 82 async fn handle_get_message( 83 State(state): State, 84 AxumPath(id): AxumPath, 85 ) -> Result, StatusCode> { 86 state 87 .store 88 .get_message(&id) 89 .map(Json) 90 .ok_or(StatusCode::NOT_FOUND) 91 } 92 93 async fn handle_send_mail( 94 State(state): State, 95 Json(req): Json, 96 ) -> Result, (StatusCode, String)> { 97 state 98 .relay 99 .send_email(req) 100 .await 101 .map(Json) 102 .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e)) 103 } 104 105 async fn handle_ingest_test( 106 State(state): State, 107 ) -> Result, (StatusCode, String)> { 108 let test_req = SendEmailRequest { 109 to: vec![state.operator_email.clone()], 110 subject: "Sovereign Mail Node Online".to_string(), 111 body: "Your sovereign mail server is running directly on Spark hardware. Messages are stored on local NVMe disk with zero cloud telemetry and synced to Atlas Cortex memory.".to_string(), 112 from: Some("atlas@sovereign.spark".to_string()), 113 }; 114 115 let id = uuid::Uuid::new_v4().to_string(); 116 let now = chrono::Utc::now().to_rfc3339(); 117 let mut headers = std::collections::HashMap::new(); 118 headers.insert("From".to_string(), "atlas@sovereign.spark".to_string()); 119 headers.insert("To".to_string(), state.operator_email.clone()); 120 headers.insert("Subject".to_string(), test_req.subject.clone()); 121 122 let mut msg = EmailMessage { 123 id: id.clone(), 124 from: "atlas@sovereign.spark".to_string(), 125 to: test_req.to, 126 subject: test_req.subject, 127 body: test_req.body, 128 headers, 129 received_at: now, 130 folder: "inbox".to_string(), 131 cortex_indexed: false, 132 }; 133 134 state 135 .store 136 .save_message(&msg) 137 .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e))?; 138 139 if state.cortex.commit_mail(&msg).await.is_ok() { 140 msg.cortex_indexed = true; 141 let _ = state.store.save_message(&msg); 142 } 143 144 Ok(Json(msg)) 145 }