File size: 1,401 Bytes
81301f1
9ce120f
3569cbd
 
2bb7b57
65f3e54
81301f1
 
ee7230e
3569cbd
 
a4dee07
ee7230e
 
 
 
 
 
 
 
9ce120f
81301f1
 
ee7230e
 
 
9ce120f
 
 
 
 
 
 
 
 
 
65f3e54
 
 
 
9ce120f
 
 
 
5b9ecd0
9ce120f
 
 
 
 
 
5b9ecd0
2bb7b57
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
use std::{env, net::IpAddr};

use config::{Config, Environment, File};
use once_cell::sync::Lazy;
use serde::Deserialize;
use tracing::debug;
#[cfg(feature = "whisper")]
use crate::whisper;

pub(crate) static SETTINGS: Lazy<Settings> =
    Lazy::new(|| Settings::new().expect("Failed to initialize settings"));


#[derive(Debug, Deserialize)]
pub(crate) struct Server {
    pub(crate) port: u16,
    pub(crate) host: IpAddr,
}

#[derive(Debug, Deserialize)]
pub struct Settings {
    #[cfg(feature = "whisper")]
    pub(crate) whisper: whisper::config::WhisperConfig,
    pub(crate) server: Server,
}

impl Settings {
    pub(crate) fn new() -> Result<Self, anyhow::Error> {
        let run_mode = env::var("APP_RUN_MODE").unwrap_or("dev".into());
        let config = Config::builder()
            .add_source(File::with_name(&format!("config/{run_mode}.yaml")).required(false))
            .add_source(Environment::with_prefix("APP").separator("-"))
            .build()
            .map_err(anyhow::Error::from)?;

        config.try_deserialize::<Self>().map_err(Into::into)
            .map(|settings| {
                debug!("Settings: {settings:?}");
                settings
            })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn load_dev_settings_should_success() {
        let settings = Settings::new().unwrap();
        println!("{:?}", settings);
    }
}