From 3022ee2c6293ff7cd83afa00578d69d3362f84af Mon Sep 17 00:00:00 2001 From: naps62-yolo Date: Sun, 24 May 2026 17:28:20 +0000 Subject: [PATCH] feat(maestro): add maestro-registry binary entry point MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes NAP-221. Adds the `maestro-registry` bin so the registry can be spawned as a child process (required by the NAP-217 E2E harness) and run as a long-lived service. - New `crates/maestro-registry/src/main.rs` with a `#[tokio::main]` entry point: loads config via `ensure_token` (override path via `MAESTRO_REGISTRY_CONFIG`), binds the configured `bind_addr`, serves the production router, and shuts down cleanly on Ctrl-C. Mirrors the `maestrod` wiring (EnvFilter tracing, ExitCode reporting). - `[lib]` + `[[bin]]` sections in `crates/maestro-registry/Cargo.toml` so the crate exposes both surfaces (same pattern as `maestro-daemon`). - `RegistryConfig` gains a `bind_addr: SocketAddr` field with `serde(default)` to default to `127.0.0.1:8080` per spec §Registry. Existing TOML files without the field still load. - `default_config_path` re-exported from the crate root for the bin. Co-Authored-By: Claude Opus 4.7 (1M context) --- Cargo.lock | 1 + crates/maestro-registry/Cargo.toml | 8 +++++ crates/maestro-registry/src/lib.rs | 2 +- crates/maestro-registry/src/main.rs | 49 ++++++++++++++++++++++++++++ crates/maestro-registry/src/token.rs | 31 ++++++++++++++++++ 5 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 crates/maestro-registry/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index d8f6bce..cac64af 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -852,6 +852,7 @@ dependencies = [ "tokio-tungstenite 0.21.0", "toml", "tracing", + "tracing-subscriber", "uuid", ] diff --git a/crates/maestro-registry/Cargo.toml b/crates/maestro-registry/Cargo.toml index 532cdba..d1edd6b 100644 --- a/crates/maestro-registry/Cargo.toml +++ b/crates/maestro-registry/Cargo.toml @@ -7,6 +7,13 @@ repository.workspace = true publish.workspace = true description = "Always-on registry — daemon presence index and session catalog" +[lib] +path = "src/lib.rs" + +[[bin]] +name = "maestro-registry" +path = "src/main.rs" + [dependencies] axum.workspace = true chrono.workspace = true @@ -21,6 +28,7 @@ thiserror.workspace = true tokio.workspace = true toml.workspace = true tracing.workspace = true +tracing-subscriber.workspace = true uuid.workspace = true [dev-dependencies] diff --git a/crates/maestro-registry/src/lib.rs b/crates/maestro-registry/src/lib.rs index 4829c6d..5884f37 100644 --- a/crates/maestro-registry/src/lib.rs +++ b/crates/maestro-registry/src/lib.rs @@ -25,4 +25,4 @@ pub use auth::{issue_cookie, verify_cookie, COOKIE_NAME}; pub use jwt::{issue_attach_jwt, ATTACH_JWT_TTL}; pub use server::{router, router_full, router_with_timeout, RegistryServer}; pub use state::{DaemonView, Registry, RegistryError, RpcError, HEARTBEAT_TIMEOUT, RPC_TIMEOUT}; -pub use token::{ensure_token, RegistryConfig, TokenError}; +pub use token::{default_config_path, ensure_token, RegistryConfig, TokenError}; diff --git a/crates/maestro-registry/src/main.rs b/crates/maestro-registry/src/main.rs new file mode 100644 index 0000000..249e97e --- /dev/null +++ b/crates/maestro-registry/src/main.rs @@ -0,0 +1,49 @@ +//! `maestro-registry` — the Maestro always-on registry binary. + +use std::error::Error; +use std::path::PathBuf; +use std::process::ExitCode; + +use maestro_registry::{default_config_path, ensure_token, router, Registry}; +use tokio::net::TcpListener; +use tracing_subscriber::EnvFilter; + +#[tokio::main] +async fn main() -> ExitCode { + let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")); + tracing_subscriber::fmt().with_env_filter(filter).init(); + + match run().await { + Ok(()) => ExitCode::SUCCESS, + Err(e) => { + tracing::error!(error = %e, "maestro-registry failed"); + ExitCode::FAILURE + } + } +} + +async fn run() -> Result<(), Box> { + let config_path: PathBuf = match std::env::var_os("MAESTRO_REGISTRY_CONFIG") { + Some(p) => PathBuf::from(p), + None => default_config_path()?, + }; + let config = ensure_token(&config_path)?; + tracing::info!(config = %config_path.display(), bind = %config.bind_addr, "loaded config"); + + let listener = TcpListener::bind(config.bind_addr).await?; + let bound = listener.local_addr()?; + tracing::info!(addr = %bound, "registry listening"); + + let registry = Registry::new(); + let app = router(registry, config.token); + + let shutdown = async { + let _ = tokio::signal::ctrl_c().await; + tracing::info!("ctrl-c received — shutting down"); + }; + + axum::serve(listener, app) + .with_graceful_shutdown(shutdown) + .await?; + Ok(()) +} diff --git a/crates/maestro-registry/src/token.rs b/crates/maestro-registry/src/token.rs index 13c8007..e746c19 100644 --- a/crates/maestro-registry/src/token.rs +++ b/crates/maestro-registry/src/token.rs @@ -6,11 +6,19 @@ //! - If absent, generates a 256-bit random token, prints it once to stderr, //! and persists it. Subsequent runs are silent. +use std::net::SocketAddr; use std::path::{Path, PathBuf}; use rand::RngCore; use serde::{Deserialize, Serialize}; +/// Default `bind_addr` when a config file omits it. +pub const DEFAULT_BIND_ADDR: &str = "127.0.0.1:8080"; + +fn default_bind_addr() -> SocketAddr { + DEFAULT_BIND_ADDR.parse().expect("valid default bind_addr") +} + #[derive(Debug, thiserror::Error)] pub enum TokenError { #[error("io: {0}")] @@ -26,6 +34,8 @@ pub enum TokenError { #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct RegistryConfig { pub token: String, + #[serde(default = "default_bind_addr")] + pub bind_addr: SocketAddr, } /// Load or create the registry config file. On creation, prints the generated @@ -42,6 +52,7 @@ pub fn ensure_token(config_path: &Path) -> Result { let token = generate_token(); let cfg = RegistryConfig { token: token.clone(), + bind_addr: default_bind_addr(), }; let body = toml::to_string_pretty(&cfg)?; std::fs::write(config_path, body)?; @@ -84,6 +95,7 @@ mod tests { let path = dir.path().join("registry.toml"); let cfg = ensure_token(&path).unwrap(); assert_eq!(cfg.token.len(), 64); // 32 bytes hex-encoded + assert_eq!(cfg.bind_addr, default_bind_addr()); let again = ensure_token(&path).unwrap(); assert_eq!(cfg, again, "second call must return same token"); @@ -97,4 +109,23 @@ mod tests { assert!(path.exists()); assert_eq!(cfg.token.len(), 64); } + + #[test] + fn legacy_config_without_bind_addr_loads_with_default() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("registry.toml"); + std::fs::write(&path, "token = \"abcd\"\n").unwrap(); + let cfg = ensure_token(&path).unwrap(); + assert_eq!(cfg.token, "abcd"); + assert_eq!(cfg.bind_addr, default_bind_addr()); + } + + #[test] + fn custom_bind_addr_is_preserved() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("registry.toml"); + std::fs::write(&path, "token = \"x\"\nbind_addr = \"0.0.0.0:9999\"\n").unwrap(); + let cfg = ensure_token(&path).unwrap(); + assert_eq!(cfg.bind_addr, "0.0.0.0:9999".parse().unwrap()); + } }