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) <noreply@anthropic.com>
This commit is contained in:
@@ -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]
|
||||
|
||||
@@ -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};
|
||||
|
||||
@@ -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<dyn Error>> {
|
||||
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(())
|
||||
}
|
||||
@@ -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<RegistryConfig, TokenError> {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user