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
app_state.rs 2.81 KiB
Newer Older
  • Learn to ignore specific revisions
  • peterrabbit's avatar
    peterrabbit committed
    use std::path::PathBuf;
    use structopt::StructOpt;
    
    #[derive(Clone)]
    pub enum AppContext {
        Debug,
        Production,
    }
    
    #[derive(Clone, StructOpt)]
    pub struct AppArgs {
        #[structopt(short = "c", long = "ctx", default_value = "debug")]
        pub context: String,
    
        #[structopt(short = "d", long = "dir")]
        pub app_storage_root: Option<PathBuf>,
    
        #[structopt(long)]
        pub load: Option<PathBuf>,
    
        #[structopt(short, long, default_value = "localhost")]
        pub host: String,
    
        #[structopt(short, long, default_value = "8080")]
        pub port: u16,
    
        #[structopt(long = "ptls", default_value = "8443")]
        pub port_tls: u16,
    
        #[structopt(long = "certs_dir", default_value = "/etc/letsencrypt/live")]
        pub ssl_certs_dir: PathBuf,
    }
    
    #[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
    }
    
    #[derive(Clone)]
    pub struct AppState {
        pub config: AppConfig,
        pub preferences: AppPreference,
    
        // authentication
        // ...
    
    peterrabbit's avatar
    peterrabbit committed
    }
    
    #[derive(Clone)]
    pub struct AppPreference {}
    
    impl AppState {
        pub fn new() -> Self {
            AppState {
                config: AppConfig::new(),
                preferences: AppPreference {},
            }
        }
    }