Skip to main content

server_watchdog/infrastructure/config/
server.rs

1use std::error::Error;
2use std::sync::Arc;
3use async_trait::async_trait;
4use derive_new::new;
5use crate::application::config::ServerConfigUseCase;
6use crate::domain::config::{Config, ServerConfig};
7use crate::domain::file_accessor::FileAccessor;
8
9#[derive(new)]
10pub struct ServerConfigAdapter {
11    config_file_accessor: Arc<dyn FileAccessor<Config> + Send + Sync>
12}
13
14#[async_trait]
15impl ServerConfigUseCase for ServerConfigAdapter {
16
17    async fn add_server(&self, server_config: ServerConfig) -> Result<(), Box<dyn Error + Send + Sync>> {
18        let mut config = self.config_file_accessor.read().await?;
19        config.servers.push(server_config);
20        self.config_file_accessor.write(&config).await?;
21        Ok(())
22    }
23
24    async fn list_server(&self) -> Result<Vec<ServerConfig>, Box<dyn Error + Send + Sync>> {
25        let config = self.config_file_accessor.read().await?;
26        Ok(config.servers)
27    }
28}