use crate::shutdown;
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr};
use tokio::sync::mpsc;
use tracing::info;
use warp::{Filter, Rejection, Reply};
const DEFAULT_PORT: u16 = 40901;
#[derive(Debug)]
pub struct Health {
pub addr: SocketAddr,
shutdown: shutdown::Shutdown,
_shutdown_complete: mpsc::UnboundedSender<()>,
}
impl Health {
pub fn new(
enable_ipv6: bool,
shutdown: shutdown::Shutdown,
shutdown_complete_tx: mpsc::UnboundedSender<()>,
) -> Self {
let addr = if enable_ipv6 {
SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), DEFAULT_PORT)
} else {
SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), DEFAULT_PORT)
};
Self {
addr,
shutdown,
_shutdown_complete: shutdown_complete_tx,
}
}
pub async fn run(&mut self) {
let health_route = warp::path!("healthy")
.and(warp::get())
.and(warp::path::end())
.and_then(Self::health_handler);
tokio::select! {
_ = warp::serve(health_route).run(self.addr) => {
info!("health server ended");
}
_ = self.shutdown.recv() => {
info!("health server shutting down");
}
}
}
async fn health_handler() -> Result<impl Reply, Rejection> {
Ok(warp::reply())
}
}