49 lines
1.6 KiB
Rust
49 lines
1.6 KiB
Rust
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<Mutex<Store>>,
|
|
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<HashMap<String, Session>>,
|
|
/// Datei mit dem Einmalpasswort des Standard-Admins; sie wird nach der
|
|
/// ersten Einrichtung gelöscht.
|
|
pub logs_dir: std::sync::OnceLock<std::path::PathBuf>,
|
|
pub bootstrap_file: std::sync::OnceLock<std::path::PathBuf>,
|
|
}
|
|
|
|
impl AppState {
|
|
pub fn new(store: Store, runtime: Runtime) -> Arc<Self> {
|
|
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()),
|
|
logs_dir: std::sync::OnceLock::new(),
|
|
bootstrap_file: std::sync::OnceLock::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())
|
|
}
|
|
}
|