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
env.rs 2.9 KiB
Newer Older
Pierre Jarriges's avatar
Pierre Jarriges committed
use std::env;

Pierre Jarriges's avatar
Pierre Jarriges committed
#[derive(Debug, Clone)]
/// Makes a copy of all required values defined in the system environment variables
pub struct Env {
    pub release_mode: String,
    pub db_username: String,
    pub db_user_pwd: String,
    pub db_name: String,
    pub db_port: String,
    pub server_host: String,
    pub crypt_key: String,
    pub default_admin_username: String,
    pub default_admin_password: String,
}

Pierre Jarriges's avatar
Pierre Jarriges committed
static RELEASE_MODES: [&str; 3] = ["debug", "test", "prod"];

pub fn get_release_mode() -> String {
    let default = "prod";

    match env::var("RELEASE_MODE") {
        Ok(s) => {
            if RELEASE_MODES.contains(&s.as_str()) {
                return s;
            } else {
                return default.to_string();
            }
        }
        Err(_) => default.to_string(),
    }
}

pub fn get_log_level() -> String {
    let rel_mode = get_release_mode();
    match rel_mode.as_str() {
        "debug" => String::from("debug"),
        "test" => String::from("debug"),
        _ => String::from("info"),
    }
}
Pierre Jarriges's avatar
Pierre Jarriges committed

impl Env {
    pub fn new() -> Env {
        Env {
            release_mode: get_release_mode(),
            db_username: env::var("DB_USERNAME").expect("DB_USERNAME is not defined."),
            db_user_pwd: env::var("DB_USER_PASSWORD").expect("DB_USER_PASSWORD is not defined."),
            db_name: env::var("DATABASE_NAME").expect("DATABASE_NAME is not defined."),
            db_port: env::var("DB_PORT").expect("DB_PORT is not defined."),
            server_host: env::var("SERVER_HOST").expect("SERVER_HOST is not defined"),
            crypt_key: env::var("CRYPT_KEY").expect("CRYPT_KEY is not defined."),
            default_admin_username: env::var("DEFAULT_ADMIN_USERNAME")
                .expect("DEFAULT_ADMIN_USERNAME is not defined"),
            default_admin_password: env::var("DEFAULT_ADMIN_PASSWORD")
                .expect("DEFAULT_ADMIN_PASSWORD is not defined"),
        }
    }

    #[cfg(test)]
    /// Returns an instance with some values adjusted for testing such as email addresses
    pub fn for_test() -> Env {
        Env {
            release_mode: String::from("debug"),
            db_username: env::var("DB_USERNAME").expect("DB_USERNAME is not defined."),
            db_user_pwd: env::var("DB_USER_PASSWORD").expect("DB_USER_PASSWORD is not defined."),
            db_name: env::var("DATABASE_NAME").expect("DATABASE_NAME is not defined."),
            db_port: env::var("DB_PORT").expect("DB_PORT is not defined."),
            server_host: env::var("SERVER_HOST").expect("SERVER_HOST is not defined"),
            crypt_key: env::var("CRYPT_KEY").expect("CRYPT_KEY is not defined."),
            default_admin_username: env::var("DEFAULT_ADMIN_USERNAME")
                .expect("DEFAULT_ADMIN_USERNAME is not defined"),
            default_admin_password: env::var("DEFAULT_ADMIN_PASSWORD")
                .expect("DEFAULT_ADMIN_PASSWORD is not defined"),
        }
    }
}