Skip to main content

server_watchdog/domain/
server.rs

1pub mod health;
2
3use crate::domain::config::ServerConfig;
4use crate::domain::server::health::HealthCheckMethod;
5
6pub struct Server {
7    pub name: String,
8    pub base_url: Option<String>,
9    pub docker_container_name: Option<String>,
10    pub health_check_method: HealthCheckMethod,
11    pub kill_path: Option<String>,
12    pub log_command: Option<Vec<String>>
13}
14
15impl Server {
16    pub fn get_health_check_url(&self) -> Option<String> {
17        let health_check_path = match &self.health_check_method {
18            HealthCheckMethod::Http(value) => value.trim_start_matches('/'),
19            _ => return None
20        };
21        Some(format!("{}/{health_check_path}", self.base_url.as_ref()?.trim_end_matches('/')))
22    }
23
24    pub fn get_kill_url(&self) -> Option<String> {
25        let kill_path = self.kill_path.as_ref()?.trim_start_matches('/');
26        Some(format!("{}/{kill_path}", self.base_url.as_ref()?.trim_end_matches('/')))
27    }
28
29    pub fn from(config: ServerConfig) -> Self {
30        let log_command = if let Some(raw_command) = config.log_command {
31            Some(raw_command.split_whitespace().map(|ref_str|{String::from(ref_str)}).collect())
32        } else { 
33            None
34        };
35
36        let health_check_method = match config.health_check_path {
37            Some(path) => HealthCheckMethod::Http(path),
38            None => {
39                if config.docker_container_name.is_some() {
40                    HealthCheckMethod::Docker
41                } else {
42                    HealthCheckMethod::None
43                }
44            }
45        };
46        
47        Self {
48            name: config.name,
49            base_url: config.base_url,
50            docker_container_name: config.docker_container_name,
51            health_check_method,
52            kill_path: config.kill_path,
53            log_command
54        }
55    }
56}