aboutsummaryrefslogtreecommitdiffstats
path: root/src/server/state/mod.rs
blob: f5f731e2ea6dc3d555770da728495ae62e1b6aa8 (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
pub mod database;
pub mod federation;

use std::sync::Arc;

use sellershut_auth::{ClientOptions, OauthClient};
#[cfg(feature = "oauth-discord")]
use url::Url;

#[cfg(feature = "oauth-discord")]
use crate::config::DiscordOauth;
use crate::{config::Config, server::driver::SellershutDriver};

#[derive(Clone)]
pub struct AppState {
    driver: Arc<dyn SellershutDriver>,
    #[cfg(feature = "oauth-discord")]
    oauth_discord: OauthClient,
}

impl AppState {
    pub async fn new(config: &Config, driver: impl SellershutDriver) -> anyhow::Result<Self> {
        Ok(Self {
            driver: Arc::new(driver),
            oauth_discord: discord_client(&config.oauth.discord, &config.oauth.oauth_redirect_url)?,
        })
    }
}

#[cfg(feature = "oauth-discord")]
fn discord_client(disc: &DiscordOauth, redirect: &Url) -> anyhow::Result<OauthClient> {
    let discord_opts = ClientOptions::builder()
        .client_id(disc.client_id.to_owned())
        .redirect_url(redirect.to_string())
        .auth_url(disc.auth_url.to_string())
        .client_secret(disc.client_secret.clone())
        .token_url(disc.token_url.to_string())
        .build();

    Ok(sellershut_auth::oauth_client(&discord_opts)?)
}