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
38 lines (30 loc) · 1.19 KB
/
Copy pathconfig.rs
File metadata and controls
38 lines (30 loc) · 1.19 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
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,
}
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()));
}
Ok(Config {
mongo_uri,
jwt_secret,
brevo_api_key,
})
}
}