aboutsummaryrefslogtreecommitdiffstats
path: root/src/config/mod.rs
blob: aa6f7702b69889118358dc0a472cfdfb0731ec2d (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
mod cli;
mod logging;
mod port;
pub use cli::Cli;
#[cfg(feature = "oauth")]
use secrecy::SecretString;
use serde::Deserialize;
use url::Url;

use crate::config::logging::LogLevel;

#[derive(Default, Deserialize, Debug, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub enum Environment {
    #[default]
    Dev,
    Prod,
}

#[derive(Debug, Deserialize, Default)]
#[serde(rename_all = "kebab-case")]
pub struct Config {
    #[serde(default)]
    pub database: DatabaseOptions,
    #[serde(default)]
    pub server: Api,
    #[serde(default)]
    #[cfg(feature = "oauth")]
    pub oauth: OAuth,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Api {
    #[serde(default = "default_domain")]
    pub domain: String,

    #[serde(default = "default_request_timeout")]
    pub request_timeout: u64,

    #[serde(default = "default_port")]
    pub port: u16,

    #[serde(default = "default_log_level")]
    pub log_level: LogLevel,

    #[serde(default = "default_sys_name")]
    pub system_name: String,

    #[serde(default)]
    pub environment: Environment,
}

#[derive(Debug, Clone, Deserialize)]
#[cfg(feature = "oauth")]
pub struct OAuth {
    #[cfg(feature = "oauth-discord")]
    pub discord: DiscordOauth,
    #[serde(rename = "redirect-url")]
    pub oauth_redirect_url: Url,
}

#[cfg(feature = "oauth-discord")]
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct DiscordOauth {
    pub client_id: String,
    pub client_secret: SecretString,
    #[serde(default = "discord_token_url")]
    pub token_url: Url,
    #[serde(default = "discord_auth_url")]
    pub auth_url: Url,
}

#[cfg(feature = "oauth-discord")]
fn discord_token_url() -> Url {
    Url::parse("https://discord.com/api/oauth2/authorize?response_type=code").expect("valid url")
}

#[cfg(feature = "oauth-discord")]
fn discord_auth_url() -> Url {
    Url::parse("https://discord.com/api/oauth2/authorize?response_type=code").expect("valid url")
}

#[cfg(feature = "oauth")]
fn redirect_url() -> Url {
    Url::parse("http://127.0.0.1:2210/auth/authorised").expect("valid url")
}

#[cfg(feature = "oauth")]
impl Default for OAuth {
    fn default() -> Self {
        Self {
            #[cfg(feature = "oauth-discord")]
            discord: DiscordOauth {
                client_id: String::default(),
                client_secret: SecretString::default(),
                token_url: discord_token_url(),
                auth_url: discord_auth_url(),
            },
            oauth_redirect_url: redirect_url(),
        }
    }
}

impl Default for Api {
    fn default() -> Self {
        Self {
            domain: default_domain(),
            request_timeout: default_request_timeout(),
            port: default_port(),
            log_level: default_log_level(),
            system_name: default_sys_name(),
            environment: Environment::default(),
        }
    }
}

#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct DatabaseOptions {
    #[serde(default = "default_database")]
    pub url: Url,
    pub pool_size: u32,
}

impl DatabaseOptions {
    pub fn create(url: &Url, pool_size: Option<u32>) -> Self {
        Self {
            url: url.to_owned(),
            pool_size: pool_size.unwrap_or_else(|| {
                let def = 100;
                tracing::debug!(size = def, "Setting default db pool size");
                def
            }),
        }
    }
}

fn default_database() -> Url {
    Url::parse("postgres://postgres:password@localhost:5432/sellershut")
        .expect("valid default DATABASE url")
}

impl Default for DatabaseOptions {
    fn default() -> Self {
        Self {
            url: default_database(),
            pool_size: 100,
        }
    }
}

fn default_sys_name() -> String {
    "sellershut".to_string()
}

fn default_domain() -> String {
    "localhost".to_string()
}

fn default_request_timeout() -> u64 {
    10
}

fn default_port() -> u16 {
    2210
}

fn default_log_level() -> LogLevel {
    LogLevel::Debug
}

impl Config {
    pub fn merge_with_cli(&mut self, cli: &Cli) {
        let server = &mut self.server;
        let dsn = &mut self.database;

        if let Some(port) = cli.port {
            server.port = port;
        }

        if let Some(domain) = &cli.domain {
            server.domain = domain.to_string();
        }

        if let Some(log_level) = &cli.log_level {
            server.log_level = *log_level;
        }

        if let Some(timeout) = cli.timeout_duration {
            server.request_timeout = timeout;
        }

        if let Some(db_url) = &cli.db {
            dsn.url = db_url.clone();
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::config::Config;

    #[test]
    fn config_file() {
        let s = include_str!("../../misc/sellershut.toml");
        assert!(toml::from_str::<Config>(s).is_ok())
    }
}