aboutsummaryrefslogblamecommitdiff
path: root/src/config.rs
blob: 4b333fe6f8bf8fdce772d73297d5d68e1bd912e3 (plain) (tree)
1
2
3
4
5
6
7
8
9
10

                       
 

                
                                    
                 
                   

                           
                           


                   










                                                   



                                    

 








                                        


                                                        
                                                                      
                                                                       
                                                                                 




                                
use config::File;
use serde::Deserialize;

use crate::auth;

#[derive(Debug, Clone, Deserialize)]
#[serde(default)]
pub struct Config {
    pub serveraddr: String,
    pub pingtimeout: i64,
    pub pingthreshold: u64,
    pub auth: Auth,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            serveraddr: "0.0.0.0:7229".to_string(),
            pingtimeout: 10,
            pingthreshold: 1,
            auth: Default::default(),
        }
    }
}

#[derive(Debug, Clone, Deserialize)]
pub struct Auth {
    pub method: auth::Methods,
    pub secret: String,
}

impl Default for Auth {
    fn default() -> Self {
        Self {
            method: auth::Methods::None,
            secret: String::new(),
        }
    }
}

impl Config {
    pub fn load() -> Result<Self, config::ConfigError> {
        let config = config::Config::builder()
            .add_source(File::with_name("/etc/webol").required(false))
            .add_source(File::with_name("config.toml").required(false))
            .add_source(config::Environment::with_prefix("WEBOL").separator("_"))
            .build()?;

        config.try_deserialize()
    }
}