diff options
Diffstat (limited to 'src/api.rs')
-rw-r--r-- | src/api.rs | 57 |
1 files changed, 5 insertions, 52 deletions
@@ -1,57 +1,10 @@ -use std::collections::HashMap; - -use axum::{extract::State, response::IntoResponse, Json}; -use serde::{Deserialize, Serialize}; - -use crate::{service::ServiceHandles, Status}; +use crate::AppState; pub mod services; +pub mod sse; -pub fn router() -> axum::Router<ServiceHandles> { - use axum::routing::get; - +pub fn router() -> axum::Router<AppState> { axum::Router::new() - .route("/healthcheck", get(healthcheck)) - .merge(services::router()) -} - -#[derive(Debug, Clone, Default, Serialize, Deserialize)] -pub struct Health { - #[serde(flatten)] - pub status: Status, - pub checks: HashMap<String, Status>, -} - -impl<T: std::error::Error> From<T> for Health { - fn from(value: T) -> Self { - Health { - status: value.into(), - ..Default::default() - } - } -} - -impl IntoResponse for Health { - fn into_response(self) -> axum::response::Response { - Json(self).into_response() - } -} - -pub async fn healthcheck(State(services): State<ServiceHandles>) -> Health { - let checks = services - .iter() - .map(|(name, srv)| (name.clone(), srv.status())) - .collect::<HashMap<String, Status>>(); - - let status = match checks - .values() - .filter(|s| !matches!(s, Status::Ok)) - .count() - { - 0 => Status::Ok, - 1 => Status::Error(Some("1 issue detected".to_string())), - n => Status::Error(Some(format!("{n} issues detected"))), - }; - - Health { status, checks } + .nest("/sse", sse::router()) + .nest("/status", services::router()) } |