Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
config.rs 1.87 KiB
Newer Older
  • Learn to ignore specific revisions
  • peterrabbit's avatar
    peterrabbit committed
    use crate::app::AppArgs;
    
    peterrabbit's avatar
    peterrabbit committed
    use std::path::PathBuf;
    use structopt::StructOpt;
    
    #[derive(Clone)]
    pub enum AppContext {
        Debug,
        Production,
    }
    
    #[derive(Clone)]
    pub struct AppConfig {
        pub exec_path: PathBuf,
        pub exec_name: String,
        pub context: AppContext,
        pub storage_dir: PathBuf,
        pub host: String,
        pub port: u16,
        pub port_tls: u16,
        pub load: Option<PathBuf>,
        pub ssl_certs_dir: PathBuf,
    }
    
    impl AppConfig {
        pub fn new() -> Self {
            let app_args = AppArgs::from_args();
            let exec_path = std::env::current_exe().unwrap();
            let exec_name = exec_path
                .file_name()
                .expect("Unable to get executable name")
                .to_str()
                .unwrap()
                .to_string();
    
            let context = match app_args.context.as_str() {
                "prod" => AppContext::Production,
                _ => AppContext::Debug,
            };
    
            let storage_dir = match &app_args.app_storage_root {
                Some(dir) => dir.join(&exec_name),
                None => match context {
                    AppContext::Production => PathBuf::from("/var").join(&exec_name),
                    AppContext::Debug => dirs::home_dir()
                        .expect("Unable to get home dir")
                        .join(&exec_name),
                },
            };
    
            let ssl_certs_dir = app_args.ssl_certs_dir.join(&app_args.host);
    
            AppConfig {
                exec_path: std::path::PathBuf::from(exec_path),
                exec_name,
                context,
                storage_dir,
                host: app_args.host,
                port: app_args.port,
                port_tls: app_args.port_tls,
                load: app_args.load,
                ssl_certs_dir,
            }
        }
    
        pub fn get_log_level(&self) -> String {
            match self.context {
                AppContext::Debug => "debug".to_string(),
                AppContext::Production => "info".to_string(),
            }
        }
    
    peterrabbit's avatar
    peterrabbit committed
    }