diff --git a/src/main.rs b/src/main.rs index 07e112a..46247be 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,7 +34,7 @@ async fn main() -> std::io::Result<()> { // get socket/port from env // backwards compat when only UDS is set - if get_env_bool("UDS") { + if utils::get_env_bool("UDS") { let socket_path = env::var("BIND_UNIX").unwrap_or_else(|_| "./socket/actix.sock".to_string()); server.bind_uds(socket_path)? @@ -74,7 +74,7 @@ static CLIENT: Lazy = Lazy::new(|| { builder }; - if get_env_bool("IPV4_ONLY") { + if utils::get_env_bool("IPV4_ONLY") { builder.local_address("0.0.0.0".parse().ok()) } else { builder @@ -126,13 +126,6 @@ fn is_header_allowed(header: &str) -> bool { ) } -fn get_env_bool(key: &str) -> bool { - match env::var(key) { - Ok(val) => val.to_lowercase() == "true" || val == "1", - Err(_) => false, - } -} - async fn index(req: HttpRequest) -> Result> { if req.method() == Method::OPTIONS { let mut response = HttpResponse::Ok(); @@ -218,7 +211,7 @@ async fn index(req: HttpRequest) -> Result> { }; #[cfg(any(feature = "webp", feature = "avif"))] - let disallow_image_transcoding = get_env_bool("DISALLOW_IMAGE_TRANSCODING"); + let disallow_image_transcoding = utils::get_env_bool("DISALLOW_IMAGE_TRANSCODING"); let rewrite = query.get("rewrite") != Some("false"); diff --git a/src/utils.rs b/src/utils.rs index 195a4e9..d5efe91 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -2,6 +2,7 @@ use qstring::QString; use reqwest::Url; use std::borrow::Cow; use std::collections::BTreeMap; +use std::env; pub fn read_buf(buf: &[u8], pos: &mut usize) -> u8 { let byte = buf[*pos]; @@ -13,7 +14,6 @@ fn finalize_url(path: &str, query: BTreeMap) -> String { #[cfg(feature = "qhash")] { use std::collections::BTreeSet; - use std::env; let qhash = { let secret = env::var("HASH_SECRET"); @@ -94,3 +94,10 @@ pub fn escape_xml(raw: &str) -> Cow<'_, str> { Cow::Owned(escaped) } } + +pub fn get_env_bool(key: &str) -> bool { + match env::var(key) { + Ok(val) => val.to_lowercase() == "true" || val == "1", + Err(_) => false, + } +}