/
mcmare
/
RustAPI
Обзор
Документация
Войти
/
mcmare
/
RustAPI
Код
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
core/src/health.rs
43 строки
1 KB
mcmare
Add core HTTP layer: axum bootstrap, auth middleware, error envelope, health route
18 июл 2026, 16:11
18 июл 2026, 16:11
ee13146
Код
Авторство
О чём код?
use axum::{extract::State, Json}; use serde::Serialize; use std::{future::Future, pin::Pin, sync::Arc}; /// A named async check (e.g. "can this DB connection be reached") returning ok/err. pub type HealthCheck = Arc<dyn Fn() -> Pin<Box<dyn Future<Output = (String, bool)> + Send>> + Send + Sync>; #[derive(Clone, Default)] pub struct HealthState { checks: Vec<HealthCheck>, } impl HealthState { pub fn new(checks: Vec<HealthCheck>) -> Self { Self { checks } } } #[derive(Serialize)] pub struct HealthResponse { status: &'static str, checks: Vec<CheckResult>, } #[derive(Serialize)] struct CheckResult { name: String, ok: bool, } pub async fn health_handler(State(state): State<HealthState>) -> Json<HealthResponse> { let mut results = Vec::with_capacity(state.checks.len()); let mut all_ok = true; for check in &state.checks { let (name, ok) = check().await; all_ok &= ok; results.push(CheckResult { name, ok }); } Json(HealthResponse { status: if all_ok { "ok" } else { "degraded" }, checks: results, }) }