From de78daf6dd1b00552ab0ee0273311b1d6834ec90 Mon Sep 17 00:00:00 2001 From: atlas Date: Sun, 29 Nov 2026 06:33:13 +0100 Subject: [PATCH] swarm-controller: hives handlers take their own sub-state via FromRef --- swarm-controller/src/routes/hives.rs | 104 +++++++++++++++++++++++++-- 1 file changed, 98 insertions(+), 6 deletions(-) diff --git a/swarm-controller/src/routes/hives.rs b/swarm-controller/src/routes/hives.rs index 6b518ad2b..dc3752eaf 100644 --- a/swarm-controller/src/routes/hives.rs +++ b/swarm-controller/src/routes/hives.rs @@ -1,12 +1,41 @@ //! The swarm's hive directory, service links and display name. +use std::sync::Arc; + use anyhow::Result; -use axum::{Json, extract::State}; +use axum::{ + Json, + extract::{FromRef, State}, +}; use serde::{Deserialize, Serialize}; use utoipa::ToSchema; use crate::routes::{AppState, StatusUnavailable}; -use crate::status; +use crate::status::{self, StatusReader}; + +/// What the directory routes may reach of [`AppState`]: the roster, the +/// service links, the swarm's display name and the queue reader. +/// +/// Derived per request from [`AppState`] (`FromRef`), so each field is a clone +/// of the same `Arc` and nothing is held twice. +#[derive(Clone)] +pub(crate) struct HivesState { + pub(crate) hives: Arc>, + pub(crate) links: Arc>, + pub(crate) swarm_name: Option>, + pub(crate) status: Option>, +} + +impl FromRef for HivesState { + fn from_ref(state: &AppState) -> Self { + Self { + hives: state.hives.clone(), + links: state.links.clone(), + swarm_name: state.swarm_name.clone(), + status: state.status.clone(), + } + } +} /// One hive in the swarm's directory — the same `name`/`domain` pair /// `services.hyperhive.swarm.hives` (nix/host-modules/swarm.nix) declares, @@ -30,7 +59,7 @@ pub(crate) struct HiveEntry { responses((status = 200, description = "every hive in the swarm", body = Vec)), tag = "hives" )] -pub(crate) async fn get_hives(State(state): State) -> Json> { +pub(crate) async fn get_hives(State(state): State) -> Json> { Json((*state.hives).clone()) } @@ -61,7 +90,7 @@ pub(crate) struct ServiceLink { responses((status = 200, description = "swarm service quick links", body = Vec)), tag = "links" )] -pub(crate) async fn get_links(State(state): State) -> Json> { +pub(crate) async fn get_links(State(state): State) -> Json> { Json((*state.links).clone()) } @@ -83,7 +112,7 @@ pub(crate) struct SwarmInfo { 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 { +pub(crate) async fn get_swarm_info(State(state): State) -> Json { Json(SwarmInfo { name: state.swarm_name.as_deref().map(str::to_owned), }) @@ -105,7 +134,7 @@ pub(crate) async fn get_swarm_info(State(state): State) -> Json, + State(state): State, ) -> Result>, StatusUnavailable> { let Some(reader) = state.status.as_ref() else { return Err(StatusUnavailable( @@ -124,3 +153,66 @@ pub(crate) async fn get_hives_status( } } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::routes::tests::{state_with_roster, state_with_roster_queued}; + use axum::http::StatusCode; + + async fn get(state: AppState, path: &str) -> (StatusCode, String) { + use axum::body::Body; + use axum::http::Request; + use tower::ServiceExt as _; + + let res = crate::startup::build_app(state) + .oneshot(Request::get(path).body(Body::empty()).expect("a request")) + .await + .expect("answers"); + let code = res.status(); + let body = axum::body::to_bytes(res.into_body(), usize::MAX) + .await + .expect("a body"); + (code, String::from_utf8_lossy(&body).into_owned()) + } + + /// Through the real router, so `FromRef` runs: each route answers with the + /// roster, links and name the app state holds. + #[tokio::test] + async fn the_directory_routes_read_the_app_state() { + let (mut state, _sched) = state_with_roster(); + state.links = Arc::new(vec![ServiceLink { + label: "forge".to_owned(), + icon: String::new(), + url: "https://forge.example".to_owned(), + }]); + state.swarm_name = Some(Arc::from("testswarm")); + + let (code, body) = get(state.clone(), "/api/hives").await; + assert_eq!(code, StatusCode::OK); + assert!(body.contains("pr1ma.example"), "{body}"); + + let (code, body) = get(state.clone(), "/api/links").await; + assert_eq!(code, StatusCode::OK); + assert!(body.contains("forge.example"), "{body}"); + + let (code, body) = get(state, "/api/swarm").await; + assert_eq!(code, StatusCode::OK); + assert!(body.contains("testswarm"), "{body}"); + } + + /// The status route reads both the queue and the roster of the app state: + /// without a queue it is 503 (presence control), with one it lists the + /// roster's hive. + #[tokio::test] + async fn the_status_route_reads_the_queue_and_roster() { + let (state, _sched) = state_with_roster(); + let (code, _) = get(state, "/api/hives/status").await; + assert_eq!(code, StatusCode::SERVICE_UNAVAILABLE); + + let (_nats, state, _sched) = state_with_roster_queued().await; + let (code, body) = get(state, "/api/hives/status").await; + assert_eq!(code, StatusCode::OK); + assert!(body.contains("pr1ma"), "{body}"); + } +}