aboutsummaryrefslogtreecommitdiffstats
path: root/crates/aggregator/src/state.rs
blob: ac4f574a50c9a87a2228b3098d8de36d92339c61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use sqlx::PgPool;
use std::{ops::Deref, sync::Arc};

use async_nats::jetstream::Context;
use warden_stack::{Configuration, cache::RedisManager};

use crate::cnfg::LocalConfig;

#[derive(Clone)]
pub struct Services {
    pub jetstream: Context,
    pub cache: RedisManager,
    pub postgres: PgPool,
}

#[derive(Clone)]
pub struct AppState {
    pub services: Services,
    pub config: LocalConfig,
}

#[derive(Clone)]
pub struct AppHandle(pub Arc<AppState>);

impl Deref for AppHandle {
    type Target = Arc<AppState>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl AppState {
    pub async fn create(
        services: Services,
        configuration: &Configuration,
    ) -> anyhow::Result<AppHandle> {
        let config = serde_json::from_value(configuration.misc.clone())?;

        Ok(AppHandle(Arc::new(Self { services, config })))
    }
}