backend/config/
data.rs

1//! Configurations for shared data that is available to all controllers.
2
3use std::sync::Arc;
4
5use actix_web::web::Data;
6use secrecy::ExposeSecret;
7
8use crate::config::app::{Config, Mode};
9use crate::db::connection;
10use crate::keycloak_api;
11use crate::sse::broadcaster::Broadcaster;
12
13/// Helper-Type - Connection pool to the database.
14pub type SharedPool = Data<connection::Pool>;
15
16/// Helper-Type - Server-Sent Events broadcaster.
17pub type SharedBroadcaster = Data<Broadcaster>;
18
19/// Helper-Type - Keycloak admin API.
20pub type SharedKeycloakApi = Data<dyn keycloak_api::traits::KeycloakApi + Send + Sync + 'static>;
21
22/// Helper-Type - Pooled HTTP client.
23pub type SharedHttpClient = Data<reqwest::Client>;
24
25/// Helper-Type - Mode of the backend.
26pub type SharedMode = Data<Mode>;
27
28/// Data-structure holding the initialized shared data.
29pub struct SharedInit {
30    /// Connection pool to the database.
31    pub pool: SharedPool,
32    /// Server-Sent Events broadcaster.
33    pub broadcaster: SharedBroadcaster,
34    /// Keycloak admin API.
35    pub keycloak_api: SharedKeycloakApi,
36    /// Pooled HTTP client.
37    pub http_client: SharedHttpClient,
38    /// Mode the backend is run in.
39    pub mode: SharedMode,
40}
41
42/// Initializes shared data.
43///
44/// # Panics
45/// If the database pool can not be initialized.
46#[must_use]
47pub fn init(config: &Config) -> SharedInit {
48    let api = Data::from(create_api(config));
49
50    SharedInit {
51        keycloak_api: api,
52        pool: Data::new(connection::init_pool(config.database_url.expose_secret())),
53        broadcaster: Data::new(Broadcaster::new()),
54        http_client: Data::new(reqwest::Client::new()),
55        mode: Data::new(config.mode),
56    }
57}
58
59/// Creates a new Keycloak API.
60/// If the admin client ID and secret are not set, a mock API is created.
61#[must_use]
62pub fn create_api(
63    config: &Config,
64) -> Arc<dyn keycloak_api::traits::KeycloakApi + Send + Sync + 'static> {
65    if let (Some(client_id), Some(client_secret)) = (
66        config.auth_admin_client_id.clone(),
67        config.auth_admin_client_secret.clone(),
68    ) {
69        Arc::new(keycloak_api::api::Api::new(keycloak_api::api::Config {
70            token_url: config.auth_token_uri.clone(),
71            client_id,
72            client_secret,
73        }))
74    } else {
75        log::info!("Using mock Keycloak API");
76        Arc::new(keycloak_api::mock_api::MockApi)
77    }
78}