This repository has been archived on 2026-10-08. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
maestro/crates/maestro-registry/src/server.rs
T
naps62-yoloandClaude Opus 4.7 18bd385a2a
ci / rust (push) Successful in 48s
feat(maestro): registry HTTP API, cookie auth, JWT issuance, events WS
Adds the client-facing surface on the registry (NAP-215). Endpoints:

- GET /api/daemons — list connected daemons (with daemon_url)
- GET /api/sessions — flat session catalog
- POST /api/sessions — forwards CreateSession to chosen daemon, 504 on
  10s daemon timeout
- DELETE /api/sessions/:id — forwards KillSession
- GET /api/sessions/:id/connect-info — returns {daemon_url, jwt} with a
  5min HS256-signed attach JWT
- WS /api/events — snapshot + delta broadcast of catalog and presence
- GET / placeholder served until NAP-218 web bundle lands

Cookie auth middleware mints a long-lived httpOnly JWT cookie on first
request and reissues silently on missing/tampered cookies. Per spec the
perimeter is the operator's network — the cookie is a second factor.

Protocol additions: daemon_url on Hello, DaemonInfo, ConnectInfo,
RegistryEvent. Daemon config + RegistryClient now carry daemon_url.

Registry state now exposes an event broadcast channel (subscribe gives
a snapshot + tokio::sync::broadcast::Receiver) and helper lookups
daemon_for_session / daemon_url.

Tests: 11 reqwest-driven HTTP integration tests cover every endpoint,
the cookie flow, 504 timeout path, worktree forwarding, and events WS
propagation.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-24 17:08:41 +00:00

111 lines
3.4 KiB
Rust

//! axum router + listener for the registry, including both the daemon-facing
//! `WS /daemon` endpoint (NAP-213) and the client-facing HTTP API (NAP-215).
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
use axum::extract::ws::WebSocketUpgrade;
use axum::extract::State;
use axum::response::IntoResponse;
use axum::routing::any;
use axum::{middleware, Router};
use tokio::net::TcpListener;
use crate::auth::{cookie_middleware, AuthState};
use crate::client_api::{routes as client_routes, ClientApiState};
use crate::handler::handle_socket;
use crate::state::{Registry, HEARTBEAT_TIMEOUT, RPC_TIMEOUT};
#[derive(Debug, Clone)]
struct DaemonAppState {
registry: Registry,
token: Arc<String>,
heartbeat_timeout: Duration,
}
/// Build the production router (daemon WS + client HTTP API) with default
/// timeouts.
pub fn router(registry: Registry, token: String) -> Router {
router_full(registry, token, HEARTBEAT_TIMEOUT, RPC_TIMEOUT)
}
/// Variant for integration tests that want a short heartbeat. Keeps the
/// existing 4-arg call sites in `daemon_ws.rs` working.
pub fn router_with_timeout(
registry: Registry,
token: String,
heartbeat_timeout: Duration,
) -> Router {
router_full(registry, token, heartbeat_timeout, RPC_TIMEOUT)
}
/// Full builder; lets tests override every knob.
pub fn router_full(
registry: Registry,
token: String,
heartbeat_timeout: Duration,
rpc_timeout: Duration,
) -> Router {
let token_arc = Arc::new(token.clone());
let daemon_router = Router::new()
.route("/daemon", any(daemon_ws))
.with_state(DaemonAppState {
registry: registry.clone(),
token: token_arc.clone(),
heartbeat_timeout,
});
let client_router = client_routes(ClientApiState {
registry,
token: token_arc.clone(),
rpc_timeout,
})
.layer(middleware::from_fn_with_state(
AuthState {
token: token_arc.clone(),
},
cookie_middleware,
));
Router::new().merge(daemon_router).merge(client_router)
}
async fn daemon_ws(State(state): State<DaemonAppState>, ws: WebSocketUpgrade) -> impl IntoResponse {
let registry = state.registry.clone();
let token = state.token.clone();
let timeout = state.heartbeat_timeout;
ws.on_upgrade(move |socket| async move {
let _ = handle_socket(socket, registry, token.as_str(), timeout).await;
})
}
/// Convenience: bind a TCP listener and serve the router on it.
#[derive(Debug)]
pub struct RegistryServer {
pub addr: SocketAddr,
pub registry: Registry,
}
impl RegistryServer {
/// Bind to `bind_addr` and serve in-place. Returns the bound `SocketAddr`
/// (so callers passing `:0` can discover it) and a handle to the registry
/// state. The future runs until the listener is dropped.
pub async fn bind(
bind_addr: SocketAddr,
token: String,
) -> std::io::Result<(Self, impl std::future::Future<Output = std::io::Result<()>>)> {
let listener = TcpListener::bind(bind_addr).await?;
let addr = listener.local_addr()?;
let registry = Registry::new();
let app = router(registry.clone(), token);
let server = Self {
addr,
registry: registry.clone(),
};
let fut = async move { axum::serve(listener, app).await };
Ok((server, fut))
}
}