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

                       
 

                
                                    
                 
                   
                     
                         
                           


                   


                          
                                             






                                     



                                    

 








                                        


                                                        
                                                                      
                                                                       
                                                                                 




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

use crate::auth;

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

impl Default for Config {
    fn default() -> Self {
        Self {
            addr: "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()
    }
}