Watch
0
0
Fork
You've already forked hyperhive
0

swarm-controller: hives handlers take their own sub-state via FromRef

This commit is contained in:
atlas 2026-11-29 06:33:13 +01:00 • committed by mara
commit de78daf6dd

View file

@ -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<Vec<HiveEntry>>,
pub(crate) links: Arc<Vec<ServiceLink>>,
pub(crate) swarm_name: Option<Arc<str>>,
pub(crate) status: Option<Arc<StatusReader>>,
}
impl FromRef<AppState> 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<HiveEntry>)),
tag = "hives"
)]
pub(crate) async fn get_hives(State(state): State<AppState>) -> Json<Vec<HiveEntry>> {
pub(crate) async fn get_hives(State(state): State<HivesState>) -> Json<Vec<HiveEntry>> {
Json((*state.hives).clone())
}
@ -61,7 +90,7 @@ pub(crate) struct ServiceLink {
responses((status = 200, description = "swarm service quick links", body = Vec<ServiceLink>)),
tag = "links"
)]
pub(crate) async fn get_links(State(state): State<AppState>) -> Json<Vec<ServiceLink>> {
pub(crate) async fn get_links(State(state): State<HivesState>) -> Json<Vec<ServiceLink>> {
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<AppState>) -> Json<SwarmInfo> {
pub(crate) async fn get_swarm_info(State(state): State<HivesState>) -> Json<SwarmInfo> {
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<AppState>) -> Json<SwarmI
tag = "hives"
)]
pub(crate) async fn get_hives_status(
State(state): State<AppState>,
State(state): State<HivesState>,
) -> Result<Json<Vec<status::HiveStatus>>, 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}");
}
}