use parking_lot::RwLock;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Instant;
use crate::channel::WebChannelHandle;
use crate::error::AppError;
use crate::middleware::RateLimiter;
use oxios_kernel::{config, KernelHandle, OxiosConfig};
#[derive(Clone)]
pub struct AppState {
pub base_url: String,
pub kernel: Arc<KernelHandle>,
pub channel: WebChannelHandle,
pub config: Arc<RwLock<OxiosConfig>>,
pub config_path: PathBuf,
pub start_time: Instant,
#[allow(dead_code)]
pub rate_limiter: RateLimiter,
}
impl std::fmt::Debug for AppState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AppState")
.field("base_url", &self.base_url)
.field("kernel", &"...")
.field("channel", &"...")
.field("config", &"...")
.field("config_path", &self.config_path)
.finish()
}
}
impl AppState {
pub async fn reload_config(&self) -> Result<(), AppError> {
let config = config::load_config(&self.config_path)
.map_err(|e| AppError::Internal(e.to_string()))?;
*self.config.write() = config;
tracing::info!("Config hot-reloaded from {}", self.config_path.display());
Ok(())
}
}