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:
1 parent
936efaf97e
commit
acedb9522e
10 files changed
+637
-16
No files matched your search
+51
-10
@@ -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() => {}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user