darpan 0.1.0

Linux developer service monitoring utility with auto-detection, real-time health checks, and interactive TUI for databases, APIs, Docker containers, and more
Documentation
use crate::health::HealthCheckerTrait;
use crate::models::{HealthResult, Service};
use async_trait::async_trait;
use std::net::{TcpStream, SocketAddr};
use std::time::{Duration, Instant};
use tracing::trace;

pub struct PortHealthChecker {
    timeout: Duration,
}

impl PortHealthChecker {
    pub fn new(timeout: Duration) -> Self {
        Self { timeout }
    }
}

#[async_trait]
impl HealthCheckerTrait for PortHealthChecker {
    async fn check(&self, service: &Service) -> HealthResult {
        let addr = SocketAddr::from(([127, 0, 0, 1], service.port));
        let start = Instant::now();
        
        match TcpStream::connect_timeout(&addr, self.timeout) {
            Ok(_) => {
                let response_time = start.elapsed().as_millis() as u64;
                trace!("Port {} is reachable ({}ms)", service.port, response_time);
                HealthResult::healthy(response_time)
            }
            Err(e) => {
                trace!("Port {} is not reachable: {}", service.port, e);
                HealthResult::unhealthy(
                    format!("Port not reachable: {}", e),
                    Some(format!("Check if service is running on port {}", service.port)),
                )
            }
        }
    }

    fn supports(&self, _service: &Service) -> bool {
        true // All services have ports
    }
}