summaryrefslogtreecommitdiffstats
path: root/crates/auth/src/state.rs
blob: 5a483c928dc6a2f273b5afe68db5f4e8c618b80e (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
use std::{ops::Deref, sync::Arc};

use stack_up::{Configuration, Services};

use crate::{
    client::{OauthClient, discord::discord_client},
    cnfg::LocalConfig,
    error::AppError,
};

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

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

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

pub struct AppState {
    pub services: Services,
    pub local_config: LocalConfig,
    pub discord_client: OauthClient,
    pub http_client: reqwest::Client,
}

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

        let discord_client = discord_client(&local_config.oauth.discord)?;

        Ok(AppHandle(Arc::new(Self {
            services,
            local_config,
            discord_client,
            http_client: reqwest::Client::new(),
        })))
    }
}