Land the shared peer contracts: types, error mapping, transport trait, fake transport and fixtures, registry/proxy/health skeletons, the AppState.peers and Config.peers seams, the ping and peers route stubs, and unit plus integration tests. No loops, no network, feature off by default.
43 lines
1.5 KiB
Rust
43 lines
1.5 KiB
Rust
//! Shared application state handed to every surface.
|
|
|
|
use std::sync::Arc;
|
|
|
|
use sqlx::SqlitePool;
|
|
use tokio_util::sync::CancellationToken;
|
|
|
|
use crate::config::Config;
|
|
use crate::peers::PeerRegistry;
|
|
use crate::sources::{DockerSource, JournalSource, SystemdSource};
|
|
|
|
/// Cheap to clone. Every stream and background task must select on
|
|
/// `shutdown` so graceful shutdown cannot hang on open SSE connections.
|
|
#[derive(Clone)]
|
|
pub struct AppState {
|
|
pub config: Arc<Config>,
|
|
pub systemd: Arc<dyn SystemdSource>,
|
|
pub journal: Arc<dyn JournalSource>,
|
|
pub docker: Arc<dyn DockerSource>,
|
|
/// `None` when auth is disabled.
|
|
pub db: Option<SqlitePool>,
|
|
/// `None` when the peers feature is off: no routes, tools, tasks or metrics.
|
|
pub peers: Option<Arc<PeerRegistry>>,
|
|
pub shutdown: CancellationToken,
|
|
}
|
|
|
|
impl AppState {
|
|
/// State backed by the real sources. Source constructors are lazy and
|
|
/// never fail, so a missing Docker socket does not prevent startup.
|
|
pub fn live(config: Config, db: Option<SqlitePool>, shutdown: CancellationToken) -> Self {
|
|
use crate::sources::{docker::BollardDocker, journal::SdJournal, systemd::DbusSystemd};
|
|
Self {
|
|
systemd: Arc::new(DbusSystemd::default()),
|
|
journal: Arc::new(SdJournal::new(&config, shutdown.clone())),
|
|
docker: Arc::new(BollardDocker::new(&config, shutdown.clone())),
|
|
config: Arc::new(config),
|
|
db,
|
|
peers: None,
|
|
shutdown,
|
|
}
|
|
}
|
|
}
|