-
Notifications
You must be signed in to change notification settings - Fork 0
/
globals.rs
57 lines (52 loc) · 1.69 KB
/
globals.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
use std::fs::File;
use std::io::Read;
use anyhow::Result;
use lazy_static::lazy_static;
use serde::Deserialize;
#[derive(Deserialize, Debug)]
pub struct Config {
pub kafka_bootstrap_server: String,
pub kafka_inbox_topic: String,
pub kafka_inbox_group_id: String,
pub kafka_inbox_num_workers: u8,
pub kafka_client_id: String,
pub http_addr: String,
pub rust_env: String,
}
impl Config {
pub fn is_production(&self) -> bool {
self.rust_env == "production"
}
pub fn is_development(&self) -> bool {
!self.is_production()
}
}
fn load_config() -> Result<Config> {
let env = envy::from_env::<Config>();
match env {
// if we could load the config using the existing env variables - use that
Ok(config) => Ok(config),
// otherwise, try to load the .env file
Err(_) => {
// simulate https://www.npmjs.com/package/dotenv behavior
let mut file = File::open(".env")?;
let mut content = String::new();
file.read_to_string(&mut content)?;
for line in content.lines() {
let pair = line.split('=').collect::<Vec<&str>>();
let (key, value) = match &pair[..] {
&[key, value] => (key, value),
_ => panic!("Expected env variable pairs, got {}", content),
};
std::env::set_var(key, value);
}
match envy::from_env::<Config>() {
Ok(config) => Ok(config),
Err(e) => panic!("Failed to read the config from env: {}", e),
}
}
}
}
lazy_static! {
pub static ref CONFIG: Config = load_config().unwrap();
}