refactor: move get_env_bool to utils.rs

This commit is contained in:
Bnyro 2024-05-08 21:53:37 +02:00
parent 3f8a33b9d6
commit 4026431403
2 changed files with 11 additions and 11 deletions

View file

@ -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<Client> = 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<HttpResponse, Box<dyn Error>> {
if req.method() == Method::OPTIONS {
let mut response = HttpResponse::Ok();
@ -218,7 +211,7 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
};
#[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");

View file

@ -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, String>) -> 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,
}
}