bws_web_server/server/
config_reload.rs

1use crate::config::ServerConfig;
2use std::sync::Arc;
3use tokio::sync::RwLock;
4
5/// A service responsible for handling configuration reloads
6#[derive(Clone)]
7pub struct ConfigReloadService {
8    config: Arc<RwLock<ServerConfig>>,
9    config_path: Arc<RwLock<Option<String>>>,
10}
11
12impl ConfigReloadService {
13    pub fn new(config: ServerConfig) -> Self {
14        Self {
15            config: Arc::new(RwLock::new(config)),
16            config_path: Arc::new(RwLock::new(None)),
17        }
18    }
19
20    pub async fn set_config_path(&self, path: String) {
21        let mut config_path = self.config_path.write().await;
22        *config_path = Some(path);
23    }
24
25    pub async fn get_config_path(&self) -> Option<String> {
26        self.config_path.read().await.clone()
27    }
28
29    pub async fn get_config(&self) -> ServerConfig {
30        self.config.read().await.clone()
31    }
32
33    pub async fn reload_config(&self) -> Result<(), Box<dyn std::error::Error>> {
34        let config_path = match self.get_config_path().await {
35            Some(path) => path,
36            None => return Err("No configuration file path set".into()),
37        };
38
39        // Load new configuration from file
40        let new_config = ServerConfig::load_from_file(&config_path)?;
41
42        // Validate new configuration
43        new_config.validate()?;
44
45        // Update configuration
46        {
47            let mut config = self.config.write().await;
48            *config = new_config;
49        }
50
51        log::info!("Configuration reloaded successfully from {}", config_path);
52        Ok(())
53    }
54}