forked from Txio-labs/txio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.rs
More file actions
83 lines (70 loc) · 2.34 KB
/
Copy pathconfig.rs
File metadata and controls
83 lines (70 loc) · 2.34 KB
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
use config::{Config as ConfigLoader, ConfigError, Environment};
use serde::Deserialize;
#[derive(Debug, Deserialize)]
pub struct Config {
pub mongo_uri: String,
pub jwt_secret: String,
pub brevo_api_key: String,
pub admin_emails: Vec<String>,
}
impl Config {
pub fn from_env() -> Result<Self, ConfigError> {
let builder = ConfigLoader::builder().add_source(Environment::default());
let config = builder.build()?;
// Require critical values, no defaults for security
let mongo_uri = config
.get_string("MONGO_URI")
.map_err(|_| ConfigError::Message("MONGO_URI must be set".into()))?;
let jwt_secret = config
.get_string("JWT_SECRET")
.map_err(|_| ConfigError::Message("JWT_SECRET must be set".into()))?;
let brevo_api_key = config
.get_string("BREVO_API_KEY")
.map_err(|_| ConfigError::Message("BREVO_API_KEY must be set".into()))?;
if jwt_secret.len() < 32 {
return Err(ConfigError::Message(
"JWT_SECRET must be at least 32 characters".into(),
));
}
let admin_emails = config
.get_string("ADMIN_EMAILS")
.map(|raw| parse_admin_emails(&raw))
.unwrap_or_default();
Ok(Config {
mongo_uri,
jwt_secret,
brevo_api_key,
admin_emails,
})
}
}
/// Parses a comma-separated ADMIN_EMAILS env var into a normalized
/// (trimmed, lower-cased, empty entries dropped) list of email addresses.
fn parse_admin_emails(raw: &str) -> Vec<String> {
raw.split(',')
.map(|s| s.trim().to_ascii_lowercase())
.filter(|s| !s.is_empty())
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_and_normalizes_admin_emails() {
let emails =
parse_admin_emails(" Admin@Example.com ,, second@example.com,THIRD@EXAMPLE.COM");
assert_eq!(
emails,
vec![
"admin@example.com".to_string(),
"second@example.com".to_string(),
"third@example.com".to_string(),
]
);
}
#[test]
fn empty_admin_emails_yields_empty_list() {
assert!(parse_admin_emails("").is_empty());
assert!(parse_admin_emails(" ").is_empty());
}
}