diff options
author | Toby Vincent <tobyv@tobyvin.dev> | 2024-10-09 18:23:58 -0500 |
---|---|---|
committer | Toby Vincent <tobyv@tobyvin.dev> | 2024-10-09 18:23:58 -0500 |
commit | b94f8e694bf01f5dba9ce2c01f589463a3dfbc69 (patch) | |
tree | c787530e63fb510db31533166edf1b9ff54be62a /src/api.rs | |
parent | 117d33fc478bf529094850b1fe40c558f04c9865 (diff) |
feat!: rewrite to use traits and streams
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()) } |