aboutsummaryrefslogtreecommitdiffstats
path: root/crates/pseudonyms/src/state.rs
blob: 82f025f258a5e7b33ce96647a45c64763428c4df (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
mod mutate;

use std::{
    net::{Ipv6Addr, SocketAddr},
    ops::Deref,
    sync::Arc,
};

use sqlx::PgPool;
use warden_stack::{Configuration, cache::RedisManager, tracing::SdkTracerProvider};

use crate::AppConfig;

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

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

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

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

#[derive(Clone)]
pub struct AppState {
    pub addr: SocketAddr,
    pub services: Services,
    pub config: Configuration,
    pub app_config: AppConfig,
    pub tracer_provider: Option<SdkTracerProvider>,
}

impl AppState {
    pub fn new(
        services: Services,
        config: Configuration,
        tracer_provider: Option<SdkTracerProvider>,
    ) -> anyhow::Result<Self> {
        let listen_address = SocketAddr::from((Ipv6Addr::UNSPECIFIED, config.application.port));

        let app_config: AppConfig = serde_json::from_value(config.misc.clone())?;

        Ok(Self {
            addr: listen_address,
            services,
            config,
            tracer_provider,
            app_config,
        })
    }
}