use std::collections::HashMap; use axum::{ extract::State, response::Json, routing::get, Router, }; use common_contracts::observability::{HealthStatus, ServiceStatus, TaskProgress}; use crate::state::{AppState, ServiceOperationalStatus}; pub fn create_router(app_state: AppState) -> Router { Router::new() .route("/health", get(health_check)) .route("/tasks", get(get_current_tasks)) .with_state(app_state) } /// [GET /health] /// Provides the current health status of the module. async fn health_check(State(state): State) -> Json { let mut details = HashMap::new(); let operational_status = state.status.read().await; let (service_status, reason) = match &*operational_status { ServiceOperationalStatus::Active => (ServiceStatus::Ok, "ok".to_string()), ServiceOperationalStatus::Degraded { reason } => { (ServiceStatus::Degraded, reason.clone()) } }; details.insert("operational_status".to_string(), reason); let status = HealthStatus { module_id: "finnhub-provider-service".to_string(), status: service_status, version: env!("CARGO_PKG_VERSION").to_string(), details, }; Json(status) } /// [GET /tasks] /// Reports all currently processing tasks and their progress. async fn get_current_tasks(State(state): State) -> Json> { let tasks: Vec = state .tasks .iter() .map(|entry| entry.value().clone()) .collect(); Json(tasks) }