//! The swarm's hive directory, service links and display name. use anyhow::Result; use axum::{Json, extract::State}; use serde::{Deserialize, Serialize}; use utoipa::ToSchema; use crate::routes::{AppState, StatusUnavailable}; use crate::status; /// One hive in the swarm's directory — the same `name`/`domain` pair /// `services.hyperhive.swarm.hives` (nix/host-modules/swarm.nix) declares, /// plus that hive's `swarm.dashboardDomains` entry, carried across /// unchanged rather than reshaped, so this stays a direct mirror of the /// nix source of truth instead of a second vocabulary for the same fields. #[derive(Clone, Debug, PartialEq, Serialize, Deserialize, ToSchema)] pub(crate) struct HiveEntry { pub(crate) name: String, pub(crate) domain: String, /// The host the hive's dashboard answers on. pub(crate) dashboard: String, } /// The swarm's hive directory — every hive, including whichever one this /// controller instance happens to run on (there is no "self" to exclude /// at the swarm level, unlike `hive-c0re`'s peer list). #[utoipa::path( get, path = "/api/hives", responses((status = 200, description = "every hive in the swarm", body = Vec)), tag = "hives" )] pub(crate) async fn get_hives(State(state): State) -> Json> { Json((*state.hives).clone()) } /// One quick link to a swarm-wide service (authelia, matrix, forge, this /// daemon's own swagger UI, …). Deliberately generic rather than named /// fields per service: each service's own nix module contributes its own /// entry to `services.hyperhive.deploy.swarm-controller.links` (same list-merge /// idiom `services.hyperhive.gateway.localNames` already uses), so adding /// a new one is a nix-only change — no new field here, no swarm-ui change. #[derive(Clone, Debug, PartialEq, Serialize, Deserialize, ToSchema)] pub(crate) struct ServiceLink { pub(crate) label: String, /// Emoji or short glyph. Empty string, not `Option`, when a /// contributing module has none — one fewer null-vs-absent case for /// the frontend to handle, and every real contributor sets one today. pub(crate) icon: String, pub(crate) url: String, } /// Quick links to swarm-wide services (authelia, matrix, forge, this /// daemon's own swagger UI, …), as contributed by each service's own nix /// module. Empty when nothing was configured to contribute — a caller /// renders 0 links the same way it renders any other count, no special /// "not configured" case. #[utoipa::path( get, path = "/api/links", responses((status = 200, description = "swarm service quick links", body = Vec)), tag = "links" )] pub(crate) async fn get_links(State(state): State) -> Json> { Json((*state.links).clone()) } /// Body of `GET /api/swarm`. #[derive(Clone, Debug, PartialEq, Serialize, Deserialize, ToSchema)] pub(crate) struct SwarmInfo { /// `None` when the operator never set `services.hyperhive.swarm.name` /// — a caller (swarm-ui's chrome) falls back to a generic label rather /// than treating this as an error. name: Option, } /// The swarm's own display name, for UI chrome (page title, nav) that /// wants to say which swarm it's showing — distinct from [`get_hives`], /// which lists the *hives inside* the swarm, not the swarm itself. #[utoipa::path( get, path = "/api/swarm", responses((status = 200, description = "the swarm's display name, if the operator set one", body = SwarmInfo)), tag = "hives" )] pub(crate) async fn get_swarm_info(State(state): State) -> Json { Json(SwarmInfo { name: state.swarm_name.as_deref().map(str::to_owned), }) } /// What each hive last said about itself, read from the swarm queue at /// request time. /// /// Every hive in the roster gets a row whether or not it has ever /// reported — see the `status` module for why absence, not presence, is /// the case this is built around. #[utoipa::path( get, path = "/api/hives/status", responses( (status = 200, description = "a row per hive, freshness derived now", body = Vec), (status = 503, description = "no swarm queue is configured here, or its store could not be read", body = String), ), tag = "hives" )] pub(crate) async fn get_hives_status( State(state): State, ) -> Result>, StatusUnavailable> { let Some(reader) = state.status.as_ref() else { return Err(StatusUnavailable( "no swarm queue is configured on this host".to_owned(), )); }; match reader .view(&state.hives, std::time::SystemTime::now()) .await { Ok(rows) => Ok(Json(rows)), Err(e) => { let detail = format!("{e:#}"); tracing::warn!(error = %detail, "reading the swarm status bucket failed"); Err(StatusUnavailable(detail)) } } }