Skip to content

Commit

Permalink
infer path_prefix from app_base
Browse files Browse the repository at this point in the history
  • Loading branch information
shouya committed Oct 29, 2024
1 parent 53481ed commit d3ce750
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 28 deletions.
16 changes: 1 addition & 15 deletions src/server/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl EndpointParam {
}

fn get_base(req: &Parts) -> Option<Url> {
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());
}

Expand Down Expand Up @@ -222,20 +222,6 @@ impl EndpointParam {
let base = base.parse().ok()?;
Some(base)
}

fn base_from_env() -> &'static Option<Url> {
use std::env;
use std::sync::OnceLock;

static APP_BASE_URL: OnceLock<Option<Url>> = 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<Request> for EndpointService {
Expand Down
57 changes: 44 additions & 13 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Box<str>> = 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<Box<str>> = 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<Option<Url>> = 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<Url> {
&APP_BASE_URL
}
}

pub use self::app_base::app_base_from_env;

#[derive(
JsonSchema, Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash,
)]
Expand Down Expand Up @@ -101,7 +132,7 @@ use std::{
num::NonZeroUsize,
sync::{
atomic::{AtomicUsize, Ordering},
LazyLock, RwLock,
RwLock,
},
time::{Duration, Instant},
};
Expand Down

0 comments on commit d3ce750

Please sign in to comment.