2019-07-27 17:14:08 +02:00
|
|
|
use crate::{config::Opt, service::Github};
|
2019-08-11 13:48:04 +02:00
|
|
|
use std::{borrow::Cow, env, time::Duration};
|
2019-07-27 17:14:08 +02:00
|
|
|
use structopt::StructOpt;
|
|
|
|
|
2019-07-24 17:54:00 +02:00
|
|
|
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
2019-08-07 21:26:12 +02:00
|
|
|
pub(crate) const REDIRECT_AGE: Duration = Duration::from_secs(5 * 60);
|
2019-07-27 16:15:11 +02:00
|
|
|
pub(crate) const FAVICON: &[u8] = include_bytes!("../static/favicon32.png");
|
2019-07-24 17:54:00 +02:00
|
|
|
lazy_static! {
|
2019-08-09 19:43:58 +02:00
|
|
|
pub(crate) static ref USER_AGENT: String = format!("yagcdn/{}", VERSION);
|
2019-07-27 17:14:08 +02:00
|
|
|
pub(crate) static ref OPT: Opt = Opt::from_args();
|
2019-08-11 13:48:04 +02:00
|
|
|
pub(crate) static ref GITHUB_AUTH_QUERY: Cow<'static, str> =
|
|
|
|
Github::auth_query().unwrap_or_default().into();
|
|
|
|
pub(crate) static ref CF_ZONE_IDENT: Cow<'static, str> = OPT
|
2019-07-28 14:45:39 +02:00
|
|
|
.cf_zone
|
2019-08-11 13:48:04 +02:00
|
|
|
.as_ref()
|
|
|
|
.map(Cow::from)
|
2019-07-28 14:54:36 +02:00
|
|
|
.or_else(|| load_env_var("CF_ZONE_IDENT"))
|
2019-08-11 13:48:04 +02:00
|
|
|
.expect("Cloudflare zone identifier not set")
|
|
|
|
.into();
|
|
|
|
pub(crate) static ref CF_AUTH_KEY: Cow<'static, str> = OPT
|
2019-07-28 14:45:39 +02:00
|
|
|
.cf_auth_key
|
2019-08-11 13:48:04 +02:00
|
|
|
.as_ref()
|
|
|
|
.map(Cow::from)
|
2019-07-28 14:54:36 +02:00
|
|
|
.or_else(|| load_env_var("CF_AUTH_KEY"))
|
2019-08-11 13:48:04 +02:00
|
|
|
.expect("Cloudflare auth key not set")
|
|
|
|
.into();
|
|
|
|
pub(crate) static ref CF_AUTH_USER: Cow<'static, str> = OPT
|
2019-07-28 14:45:39 +02:00
|
|
|
.cf_auth_user
|
2019-08-11 13:48:04 +02:00
|
|
|
.as_ref()
|
|
|
|
.map(Cow::from)
|
2019-07-28 14:54:36 +02:00
|
|
|
.or_else(|| load_env_var("CF_AUTH_USER"))
|
2019-08-11 13:48:04 +02:00
|
|
|
.expect("Cloudflare auth user not set")
|
|
|
|
.into();
|
|
|
|
pub(crate) static ref HOSTNAME: Cow<'static, str> = OPT
|
2019-07-28 14:45:39 +02:00
|
|
|
.hostname
|
2019-08-11 13:48:04 +02:00
|
|
|
.as_ref()
|
|
|
|
.map(Cow::from)
|
2019-08-09 19:43:58 +02:00
|
|
|
.or_else(|| load_env_var("YAGCDN_HOSTNAME"))
|
2019-08-11 13:48:04 +02:00
|
|
|
.unwrap_or_else(|| "yagcdn.tk".into());
|
2019-07-24 17:54:00 +02:00
|
|
|
}
|
2019-07-28 14:54:36 +02:00
|
|
|
|
2019-08-11 13:48:04 +02:00
|
|
|
pub(crate) fn load_env_var(key: &str) -> Option<Cow<'static, str>> {
|
|
|
|
env::var(key).ok().and_then(|val| {
|
|
|
|
if val.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(val.into())
|
|
|
|
}
|
|
|
|
})
|
2019-07-28 14:54:36 +02:00
|
|
|
}
|