Properly load env vars

This commit is contained in:
Valentin Brandl 2019-07-28 14:54:36 +02:00
parent 4fc8c7ce98
commit c05c2ffabf
No known key found for this signature in database
GPG Key ID: 30D341DD34118D7D
2 changed files with 13 additions and 8 deletions

View File

@ -1,6 +1,6 @@
use crate::{
data::FilePath,
statics::{GITHUB_AUTH_QUERY, OPT},
statics::{load_env_var, GITHUB_AUTH_QUERY, OPT},
};
use actix_web::{
http::{header::LOCATION, StatusCode},
@ -109,14 +109,13 @@ pub(crate) struct Github;
impl Github {
pub(crate) fn auth_query() -> Option<String> {
use std::env::var;
OPT.github_id
.clone()
.or_else(|| var("GITHUB_CLIENT_ID").ok())
.or_else(|| load_env_var("GITHUB_CLIENT_ID"))
.and_then(|id| {
OPT.github_secret
.clone()
.or_else(|| var("GITHUB_CLIENT_SECRET").ok())
.or_else(|| load_env_var("GITHUB_CLIENT_SECRET"))
.map(|secret| format!("?client_id={}&client_secret={}", id, secret))
})
}

View File

@ -11,21 +11,27 @@ lazy_static! {
pub(crate) static ref CF_ZONE_IDENT: String = OPT
.cf_zone
.clone()
.or_else(|| env::var("CF_ZONE_IDENT").ok())
.or_else(|| load_env_var("CF_ZONE_IDENT"))
.expect("Cloudflare zone identifier not set");
pub(crate) static ref CF_AUTH_KEY: String = OPT
.cf_auth_key
.clone()
.or_else(|| env::var("CF_AUTH_KEY").ok())
.or_else(|| load_env_var("CF_AUTH_KEY"))
.expect("Cloudflare auth key not set");
pub(crate) static ref CF_AUTH_USER: String = OPT
.cf_auth_user
.clone()
.or_else(|| env::var("CF_AUTH_USER").ok())
.or_else(|| load_env_var("CF_AUTH_USER"))
.expect("Cloudflare auth user not set");
pub(crate) static ref HOSTNAME: String = OPT
.hostname
.clone()
.or_else(|| env::var("GITACHE_HOSTNAME").ok())
.or_else(|| load_env_var("GITACHE_HOSTNAME"))
.unwrap_or_else(|| "gitcdn.tk".to_string());
}
pub(crate) fn load_env_var(key: &str) -> Option<String> {
env::var(key)
.ok()
.and_then(|val| if val.is_empty() { None } else { Some(val) })
}