agentmux_srv/server/containers_handler.rs
1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! `GET /api/v1/containers`: this machine's agent containers and the container runtime's state, as names and
5//! states only (`backend::container_status`). What is there now: a removed container is only ever an event.
6//! Read-only, and it reaches nothing inside a container.
7//!
8//! The runtime is looked at live, as `containerruntimeavailable` does, so a reader finds a runtime that started
9//! after srv did and wakes the watcher. Where there is no container runtime this answers 200 with an empty
10//! list and `runtime.available: false`, never an error.
11
12use axum::extract::State;
13use axum::response::{IntoResponse, Json};
14
15use crate::backend::container_status::{self, ContainersList, DockerSource, Source as _};
16use crate::server::AppState;
17
18pub async fn handle_containers(State(state): State<AppState>) -> impl IntoResponse {
19 let Some(hub) = container_status::hub() else {
20 return Json(ContainersList { runtime: container_status::ContainerRuntimeStatus::absent(), containers: Vec::new() });
21 };
22 let probe = DockerSource::new(state.container_manager.clone()).ping().await;
23 let available = probe.available;
24 hub.on_runtime(probe);
25 if available {
26 hub.wake();
27 }
28 Json(hub.list())
29}