use std::net::{Shutdown, TcpStream};
use crate::config_models::{Service, ServiceProtocol, Services};
use crate::log;
pub async fn are_running(services: &Services) -> bool {
if !services.is_empty() {
log::print("Checking Services ...".to_string());
for service in services {
if !is_running(service).await {
log::error(format!("\tService '{}' is not running.", service.name));
return false;
} else {
log::print(format!("\tService '{}' is running.", service.name));
}
}
}
true
}
async fn is_running(service: &Service) -> bool {
return match service.protocol {
ServiceProtocol::HTTP => match reqwest::get(service.uri.as_str()).await {
Ok(response) => response.status().is_success(),
Err(e) => {
log::error(format!(
"\tService '{}' is not reachable: {}",
service.name, e
));
false
}
},
ServiceProtocol::TCP => match TcpStream::connect(service.uri.as_str()) {
Ok(stream) => {
let _ = stream.shutdown(Shutdown::Both);
true
}
Err(e) => {
log::error(format!(
"\tService '{}' is not reachable: {}",
service.name, e
));
false
}
},
};
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config_models::{Service, ServiceProtocol};
#[tokio::test]
async fn cloudflare_is_running() {
let test_service = Service {
name: "cloudflare".to_string(),
protocol: ServiceProtocol::HTTP,
uri: "https://cloudflare.com".to_string(),
};
assert!(is_running(&test_service).await);
}
#[tokio::test]
async fn cloudflare_tcp_ping() {
let test_service = Service {
name: "cloudflare tcp".to_string(),
protocol: ServiceProtocol::TCP,
uri: "cloudflare.com:80".to_string(),
};
assert!(is_running(&test_service).await);
}
#[tokio::test]
async fn ip_172_32_137_254_port_31330_is_offline() {
let test_service = Service {
name: "172.32.137.254:31330".to_string(),
protocol: ServiceProtocol::HTTP,
uri: "http://172.32.137.254:31330/status".to_string(),
};
assert_eq!(is_running(&test_service).await, false);
}
#[tokio::test]
async fn ip_172_32_137_254_port_31330_wont_tcp_ping() {
let test_service = Service {
name: "172.32.137.254:31330".to_string(),
protocol: ServiceProtocol::TCP,
uri: "172.32.137.254:31330".to_string(),
};
assert_eq!(is_running(&test_service).await, false);
}
}