From d3ce750d0201c8ea5c470d19a46c3d2bd3303db4 Mon Sep 17 00:00:00 2001 From: shouya <526598+shouya@users.noreply.github.com> Date: Tue, 29 Oct 2024 11:22:16 +0900 Subject: [PATCH] infer path_prefix from app_base --- src/server/endpoint.rs | 16 +----------- src/util.rs | 57 ++++++++++++++++++++++++++++++++---------- 2 files changed, 45 insertions(+), 28 deletions(-) diff --git a/src/server/endpoint.rs b/src/server/endpoint.rs index fb190ed..e8340fd 100644 --- a/src/server/endpoint.rs +++ b/src/server/endpoint.rs @@ -188,7 +188,7 @@ impl EndpointParam { } fn get_base(req: &Parts) -> Option { - if let Some(url) = Self::base_from_env().as_ref() { + if let Some(url) = crate::util::app_base_from_env().as_ref() { return Some(url.clone()); } @@ -222,20 +222,6 @@ impl EndpointParam { let base = base.parse().ok()?; Some(base) } - - fn base_from_env() -> &'static Option { - use std::env; - use std::sync::OnceLock; - - static APP_BASE_URL: OnceLock> = OnceLock::new(); - APP_BASE_URL.get_or_init(|| { - let var = env::var("RSS_FUNNEL_APP_BASE").ok(); - var.map(|v| { - v.parse() - .expect("Invalid base url specified in RSS_FUNNEL_APP_BASE") - }) - }) - } } impl Service for EndpointService { diff --git a/src/util.rs b/src/util.rs index 9f31b70..d70b404 100644 --- a/src/util.rs +++ b/src/util.rs @@ -24,20 +24,51 @@ pub fn is_env_set(name: &str) -> bool { matches!(val.as_str(), "1" | "t" | "true" | "y" | "yes") } -const DEFAULT_PATH_PREFIX: &str = "/"; -static PATH_PREFIX: LazyLock> = LazyLock::new(|| { - let prefix = std::env::var("RSS_FUNNEL_PATH_PREFIX") - .unwrap_or_else(|_| DEFAULT_PATH_PREFIX.to_owned()) - .into_boxed_str(); - assert!(prefix.ends_with("/")); - prefix -}); - -pub fn relative_path(path: &str) -> String { - debug_assert!(!path.starts_with("/")); - format!("{}{path}", *PATH_PREFIX) +mod path_prefix { + use std::sync::LazyLock; + + const DEFAULT_PATH_PREFIX: &str = "/"; + pub static PATH_PREFIX: LazyLock> = LazyLock::new(|| { + let prefix = std::env::var("RSS_FUNNEL_PATH_PREFIX") + .ok() + .or_else(|| { + super::app_base_from_env() + .as_ref() + .map(|url| url.path().to_owned()) + }) + .unwrap_or_else(|| DEFAULT_PATH_PREFIX.to_owned()) + .into_boxed_str(); + assert!(prefix.ends_with("/")); + prefix + }); + + pub fn relative_path(path: &str) -> String { + debug_assert!(!path.starts_with("/")); + format!("{}{path}", *PATH_PREFIX) + } +} + +pub use self::path_prefix::relative_path; + +mod app_base { + use std::sync::LazyLock; + use url::Url; + + static APP_BASE_URL: LazyLock> = LazyLock::new(|| { + let var = std::env::var("RSS_FUNNEL_APP_BASE").ok(); + var.map(|v| { + v.parse() + .expect("Invalid base url specified in RSS_FUNNEL_APP_BASE") + }) + }); + + pub fn app_base_from_env() -> &'static Option { + &APP_BASE_URL + } } +pub use self::app_base::app_base_from_env; + #[derive( JsonSchema, Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash, )] @@ -101,7 +132,7 @@ use std::{ num::NonZeroUsize, sync::{ atomic::{AtomicUsize, Ordering}, - LazyLock, RwLock, + RwLock, }, time::{Duration, Instant}, };