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;
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/// Data-structure holding the initialized shared data.
26pub struct SharedInit {
27    /// Connection pool to the database.
28    pub pool: SharedPool,
29    /// Server-Sent Events broadcaster.
30    pub broadcaster: SharedBroadcaster,
31    /// Keycloak admin API.
32    pub keycloak_api: SharedKeycloakApi,
33    /// Pooled HTTP client.
34    pub http_client: SharedHttpClient,
35}
36
37/// Initializes shared data.
38///
39/// # Panics
40/// If the database pool can not be initialized.
41#[must_use]
42pub fn init(config: &Config) -> SharedInit {
43    let api = Data::from(create_api(config));
44
45    SharedInit {
46        keycloak_api: api,
47        pool: Data::new(connection::init_pool(config.database_url.expose_secret())),
48        broadcaster: Data::new(Broadcaster::new()),
49        http_client: Data::new(reqwest::Client::new()),
50    }
51}
52
53/// Creates a new Keycloak API.
54/// If the admin client ID and secret are not set, a mock API is created.
55#[must_use]
56pub fn create_api(
57    config: &Config,
58) -> Arc<dyn keycloak_api::traits::KeycloakApi + Send + Sync + 'static> {
59    if let (Some(client_id), Some(client_secret)) = (
60        config.auth_admin_client_id.clone(),
61        config.auth_admin_client_secret.clone(),
62    ) {
63        Arc::new(keycloak_api::api::Api::new(keycloak_api::api::Config {
64            token_url: config.auth_token_uri.clone(),
65            client_id,
66            client_secret,
67        }))
68    } else {
69        log::info!("Using mock Keycloak API");
70        Arc::new(keycloak_api::mock_api::MockApi)
71    }
72}