39 lines
1.3 KiB
Rust
39 lines
1.3 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::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>,
|
|
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::new(&config, shutdown.clone())),
|
|
journal: Arc::new(SdJournal::new(&config, shutdown.clone())),
|
|
docker: Arc::new(BollardDocker::new(&config, shutdown.clone())),
|
|
config: Arc::new(config),
|
|
db,
|
|
shutdown,
|
|
}
|
|
}
|
|
}
|