use std::collections::HashMap; use std::sync::{Arc, Mutex, MutexGuard}; use std::time::Instant; use nexus_store::Store; use crate::runtime::Runtime; use crate::ui::Session; /// Gemeinsamer Zustand aller Anfragen. pub struct AppState { store: Arc>, pub runtime: Runtime, pub http: reqwest::Client, /// Start des Gateways (für die Laufzeit in der Übersicht). pub started: Instant, /// Sitzungen der Weboberfläche (nur im Speicher; ein Neustart meldet ab). pub sessions: Mutex>, } impl AppState { pub fn new(store: Store, runtime: Runtime) -> Arc { let http = reqwest::Client::builder() .connect_timeout(std::time::Duration::from_secs(5)) .build() .expect("HTTP-Client konnte nicht gebaut werden"); let store = Arc::new(Mutex::new(store)); runtime.attach_store(store.clone()); Arc::new(Self { store, runtime, http, started: Instant::now(), sessions: Mutex::new(HashMap::new()), }) } /// Zugriff auf die Datenbank. Nie über ein `await` hinweg halten. pub fn store(&self) -> MutexGuard<'_, Store> { self.store.lock().unwrap_or_else(|e| e.into_inner()) } }