rustsec_admin/
application.rs

1//! `rustsec-admin` Abscissa [`Application`] type.
2//!
3//! <https://github.com/iqlusioninc/abscissa/>
4
5use std::ops::Deref;
6use std::sync::Arc;
7
8use crate::{commands::AdminCmd, config::AppConfig};
9use abscissa_core::{
10    application::{self, AppCell},
11    config::{self, CfgCell},
12    trace, Application, FrameworkError, StandardPaths,
13};
14
15/// Application state
16pub static APPLICATION: AppCell<AdminApp> = AppCell::new();
17
18/// Obtain a read-only (multi-reader) lock on the application state.
19///
20/// Panics if the application state has not been initialized.
21pub fn app_reader() -> &'static AdminApp {
22    APPLICATION.deref()
23}
24
25/// Obtain an exclusive mutable lock on the application state.
26pub fn app_writer() -> &'static AdminApp {
27    APPLICATION.deref()
28}
29
30/// Obtain a read-only (multi-reader) lock on the application configuration.
31///
32/// Panics if the application configuration has not been loaded.
33pub fn app_config() -> config::Reader<AppConfig> {
34    APPLICATION.config.read()
35}
36
37/// `rustsec-admin` Abscissa [`Application`] type
38#[derive(Debug, Default)]
39pub struct AdminApp {
40    /// Application configuration.
41    config: CfgCell<AppConfig>,
42
43    /// Application state.
44    state: application::State<Self>,
45}
46
47impl Application for AdminApp {
48    /// Entrypoint command for this application.
49    type Cmd = AdminCmd;
50
51    /// Application configuration.
52    type Cfg = AppConfig;
53
54    /// Paths to resources within the application.
55    type Paths = StandardPaths;
56
57    /// Accessor for application configuration.
58    fn config(&self) -> Arc<AppConfig> {
59        self.config.read()
60    }
61
62    /// Borrow the application state immutably.
63    fn state(&self) -> &application::State<Self> {
64        &self.state
65    }
66
67    /// Register all components used by this application.
68    fn register_components(&mut self, command: &Self::Cmd) -> Result<(), FrameworkError> {
69        let components = self.framework_components(command)?;
70        self.state.components_mut().register(components)
71    }
72
73    /// Post-configuration lifecycle callback.
74    fn after_config(&mut self, config: Self::Cfg) -> Result<(), FrameworkError> {
75        // Configure components
76        self.state.components_mut().after_config(&config)?;
77        self.config.set_once(config);
78        Ok(())
79    }
80
81    /// Get tracing configuration from command-line options
82    fn tracing_config(&self, command: &AdminCmd) -> trace::Config {
83        if command.verbose {
84            trace::Config::verbose()
85        } else {
86            trace::Config::default()
87        }
88    }
89}