From c05c2ffabf77a0708f2117a02373940453f37753 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Sun, 28 Jul 2019 14:54:36 +0200 Subject: [PATCH] Properly load env vars --- backend/src/service.rs | 7 +++---- backend/src/statics.rs | 14 ++++++++++---- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/backend/src/service.rs b/backend/src/service.rs index ddae8a8..f9cd62f 100644 --- a/backend/src/service.rs +++ b/backend/src/service.rs @@ -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 { - 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)) }) } diff --git a/backend/src/statics.rs b/backend/src/statics.rs index a45d509..93da011 100644 --- a/backend/src/statics.rs +++ b/backend/src/statics.rs @@ -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 { + env::var(key) + .ok() + .and_then(|val| if val.is_empty() { None } else { Some(val) }) +}