-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.rs
79 lines (68 loc) · 2.1 KB
/
settings.rs
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
use anyhow::Result;
use config::{Config, ConfigError, Environment, File};
use serde::Deserialize;
use std::net::SocketAddr;
use std::path::Path;
use tracing::info;
#[derive(Clone, Debug, Deserialize)]
pub struct Redis {
pub address: SocketAddr,
pub pool_size: usize,
}
#[derive(Clone, Debug, Deserialize)]
pub struct Server {
pub listen_address: SocketAddr,
pub cors_origins: Option<Vec<String>>,
pub cors_allow_any_origin: bool,
pub path_prefix: Option<String>,
}
#[derive(Clone, Debug, Deserialize)]
pub struct Metrics {
pub auth_enabled: bool,
pub auth_username: Option<String>,
pub auth_password: Option<String>,
}
impl Metrics {
pub fn make_basic_auth_header(self) -> String {
let auth_string = [
self.auth_username.unwrap_or_default(),
":".to_string(),
self.auth_password.unwrap_or_default(),
]
.concat();
let encoded = base64::encode(auth_string);
["Basic ".to_string(), encoded].concat()
}
}
#[derive(Clone, Debug, Deserialize)]
pub struct Channel {
pub api_keys: Option<Vec<String>>,
pub secret_key: String,
pub ttl: u16,
}
#[derive(Clone, Debug, Deserialize)]
pub struct Settings {
pub redis: Redis,
pub server: Server,
pub channel: Channel,
pub metrics: Metrics,
}
impl Settings {
pub fn new(config_file: Option<&Path>) -> Result<Self, ConfigError> {
let mut s = Config::new();
// Set defaults.
s.set_default("redis.address", "127.0.0.1:6379")?;
s.set_default("redis.pool_size", 1024)?;
s.set_default("server.listen_address", "0.0.0.0:8080")?;
s.set_default("server.cors_allow_any_origin", false)?;
s.set_default("channel.ttl", 3600)?;
s.set_default("channel.secret_key", "WAEgmUZx6H".to_string())?;
s.set_default("metrics.auth_enabled", false)?;
if let Some(config_file) = config_file {
info!("Reading config file: {:?}", config_file);
s.merge(File::from(config_file))?;
}
s.merge(Environment::with_prefix("WC").separator("__"))?;
s.try_into()
}
}