use axum::{extract::State, routing::get, Json, Router};
use mlua_swarm::blueprint::store::BlueprintStore;
use serde::Serialize;
use std::sync::Arc;
#[derive(Clone, Serialize)]
pub struct DoctorInfo {
pub bind: String,
pub blueprint_backend: String,
pub blueprint_store_root: Option<String>,
pub blueprint_ref_base: Option<String>,
pub enhance_flow_enabled: bool,
pub seed_blueprint_id: String,
}
#[derive(Clone)]
struct DoctorState {
info: Arc<DoctorInfo>,
store: Arc<dyn BlueprintStore>,
}
pub fn build_doctor_router(info: DoctorInfo, store: Arc<dyn BlueprintStore>) -> Router {
let state = DoctorState {
info: Arc::new(info),
store,
};
Router::new()
.route("/v1/doctor", get(doctor_get))
.with_state(state)
}
#[derive(Serialize)]
struct DoctorResponse {
#[serde(flatten)]
info: DoctorInfo,
registered_blueprint_ids: Vec<String>,
registered_blueprint_count: usize,
}
async fn doctor_get(State(state): State<DoctorState>) -> Json<DoctorResponse> {
let mut ids: Vec<String> = state
.store
.list_ids()
.await
.map(|v| v.into_iter().map(|id| id.to_string()).collect())
.unwrap_or_default();
ids.sort();
let count = ids.len();
Json(DoctorResponse {
info: (*state.info).clone(),
registered_blueprint_ids: ids,
registered_blueprint_count: count,
})
}