1
use server::config::{ConfigError, config};
2

            
3
fn get_env_var(var_name: &str) -> String {
4
    std::env::var(var_name).unwrap_or_else(|_| panic!("{var_name} must be set"))
5
}
6

            
7
#[derive(Debug, Clone)]
8
pub struct Config {
9
    pub site_url: String,
10

            
11
    pub redis_url: String,
12
    pub client_origin: String,
13

            
14
    pub access_token_private_key: String,
15
    pub access_token_public_key: String,
16
    pub access_token_expires_in: String,
17
    pub access_token_max_age: i64,
18

            
19
    pub refresh_token_private_key: String,
20
    pub refresh_token_public_key: String,
21
    pub refresh_token_expires_in: String,
22
    pub refresh_token_max_age: i64,
23
}
24

            
25
impl Config {
26
    pub async fn init() -> Result<Config, ConfigError> {
27
        let site_url = config("site_url")
28
            .await?
29
            .ok_or(ConfigError::NoConfig("site_url".to_string()))?
30
            .to_string();
31

            
32
        let client_origin = config("client_origin")
33
            .await?
34
            .ok_or(ConfigError::NoConfig("client_origin".to_string()))?
35
            .to_string();
36

            
37
        let redis_url = config("redis_url")
38
            .await?
39
            .ok_or(ConfigError::NoConfig("redis_url".to_string()))?
40
            .to_string();
41

            
42
        let access_token_private_key = config("access_token_private_key")
43
            .await?
44
            .ok_or(ConfigError::NoConfig(
45
                "access_token_private_key".to_string(),
46
            ))?
47
            .to_string();
48

            
49
        let access_token_public_key = config("access_token_public_key")
50
            .await?
51
            .ok_or(ConfigError::NoConfig("access_token_public_key".to_string()))?
52
            .to_string();
53

            
54
        let access_token_expires_in = config("access_token_expired_in")
55
            .await?
56
            .ok_or(ConfigError::NoConfig("access_token_expired_in".to_string()))?
57
            .to_string();
58

            
59
        let access_token_max_age = config("access_token_maxage")
60
            .await?
61
            .ok_or(ConfigError::NoConfig("access_token_maxage".to_string()))?
62
            .to_string();
63

            
64
        let refresh_token_private_key = config("refresh_token_private_key")
65
            .await?
66
            .ok_or(ConfigError::NoConfig(
67
                "refresh_token_private_key".to_string(),
68
            ))?
69
            .to_string();
70

            
71
        let refresh_token_public_key = config("refresh_token_public_key")
72
            .await?
73
            .ok_or(ConfigError::NoConfig(
74
                "refresh_token_public_key".to_string(),
75
            ))?
76
            .to_string();
77

            
78
        let refresh_token_expires_in = config("refresh_token_expired_in")
79
            .await?
80
            .ok_or(ConfigError::NoConfig(
81
                "refresh_token_expired_in".to_string(),
82
            ))?
83
            .to_string();
84

            
85
        let refresh_token_max_age = config("refresh_token_maxage")
86
            .await?
87
            .ok_or(ConfigError::NoConfig("refresh_token_maxage".to_string()))?
88
            .to_string();
89

            
90
        Ok(Config {
91
            site_url,
92
            redis_url,
93
            client_origin,
94
            access_token_private_key,
95
            access_token_public_key,
96
            refresh_token_private_key,
97
            refresh_token_public_key,
98
            access_token_expires_in,
99
            refresh_token_expires_in,
100
            access_token_max_age: access_token_max_age.parse::<i64>().unwrap(),
101
            refresh_token_max_age: refresh_token_max_age.parse::<i64>().unwrap(),
102
        })
103
    }
104
}