P3: wire serve end to end, integration tests, unit file, README

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YShymaPrXmc3KYRUg2HMSs
This commit is contained in:
ariandClaude Sonnet 5.5 committed 2026-10-15 19:21:53 -04:00
1 parent 936efaf97e
commit acedb9522e
10 files changed
+637 -16

No files matched your search

+1
View File
@@ -9,6 +9,7 @@ pub mod config;
pub mod db;
pub mod error;
pub mod mcp;
pub mod server;
pub mod sources;
pub mod state;
pub mod telemetry;
+51 -10
View File
@@ -1,15 +1,18 @@
//! `slinuxm` entry point: parse CLI, load layered config, dispatch.
//!
//! Phase 3 (I1) replaces the body of `serve` with the real wiring.
use std::net::IpAddr;
use std::path::PathBuf;
use anyhow::{bail, Context, Result};
use std::net::SocketAddr;
use anyhow::{Context, Result};
use clap::{Parser, Subcommand};
use tokio_util::sync::CancellationToken;
use skyg_linux_tracker::auth::tokens::{self, TokenCmd};
use skyg_linux_tracker::config::{CliOverrides, Config};
use skyg_linux_tracker::state::AppState;
use skyg_linux_tracker::{db, server, telemetry};
#[derive(Debug, Parser)]
#[command(name = "slinuxm", version, about = "Read-only Linux system monitor")]
@@ -97,11 +100,49 @@ async fn main() -> Result<()> {
}
async fn serve(config: Config) -> Result<()> {
// Phase 3 (I1): telemetry::init, db::open, AppState::live, router wiring,
// graceful shutdown. Until then, config validation is all `serve` does.
bail!(
"server wiring is not implemented yet (P3 I1); config is valid, bind = {}:{}",
config.server.bind,
config.server.port
)
let guard = telemetry::init(&config).context("initialising telemetry")?;
let db = if config.auth.enabled {
Some(db::open(&config).await?)
} else {
tracing::warn!("auth is disabled: all endpoints are open (loopback only)");
None
};
let addr = SocketAddr::new(config.server.bind, config.server.port);
let listener = tokio::net::TcpListener::bind(addr)
.await
.with_context(|| format!("binding {addr}"))?;
let shutdown = CancellationToken::new();
let state = AppState::live(config, db, shutdown.clone());
let exporters = telemetry::spawn_exporters(state.clone());
let pool = state.db.clone();
let app = server::build_router(state);
let signal_token = shutdown.clone();
tokio::spawn(async move {
wait_for_signal().await;
tracing::info!("shutdown signal received");
signal_token.cancel();
});
let result = server::serve_listener(listener, app, shutdown.clone()).await;
// Reached on signal or server error: stop everything, then flush OTel.
shutdown.cancel();
for h in exporters {
let _ = h.await;
}
guard.shutdown().await;
if let Some(pool) = pool {
pool.close().await;
}
result
}
async fn wait_for_signal() {
use tokio::signal::unix::{signal, SignalKind};
let mut term = signal(SignalKind::terminate()).expect("SIGTERM handler");
tokio::select! {
_ = term.recv() => {}
_ = tokio::signal::ctrl_c() => {}
}
}
+37
View File
@@ -0,0 +1,37 @@
//! Router assembly and the serve loop (P3). `build_router` is used by both
//! `slinuxm serve` and the integration tests.
use anyhow::{Context, Result};
use axum::routing::get;
use axum::Router;
use tokio_util::sync::CancellationToken;
use tower_http::trace::TraceLayer;
use crate::state::AppState;
use crate::{api, auth, mcp};
/// `/healthz` public; `/api/v1/*` and `/mcp` behind the auth layer.
pub fn build_router(state: AppState) -> Router {
let protected = Router::new()
.nest("/api/v1", api::router(state.clone()))
.nest_service("/mcp", mcp::service(state.clone()))
.layer(auth::layer(state));
Router::new()
.route("/healthz", get(api::healthz))
.merge(protected)
.layer(TraceLayer::new_for_http())
}
/// Serve `app` on `listener` until `shutdown` is cancelled.
pub async fn serve_listener(
listener: tokio::net::TcpListener,
app: Router,
shutdown: CancellationToken,
) -> Result<()> {
let addr = listener.local_addr().context("local_addr")?;
tracing::info!(%addr, "listening");
axum::serve(listener, app)
.with_graceful_shutdown(async move { shutdown.cancelled().await })
.await
.context("server error")
}