pub fn url(gateway: &str) -> &'static str {
let meta = crate::runtime::core().app_meta();
let Some(gw) = meta.gateway(gateway) else {
panic!("encore: no URL configured for gateway {gateway:?}: the app has no gateway with that name")
};
first_origin(Some(&gw.origins)).unwrap_or_else(|| {
panic!(
"encore: no URL configured for gateway {gateway:?}: this environment provides no origins for it"
)
})
}
pub(crate) fn first_origin(origins: Option<&[String]>) -> Option<&str> {
let url = origins?.first()?.trim_end_matches('/');
(!url.is_empty()).then_some(url)
}
#[cfg(test)]
mod tests {
use super::first_origin;
fn origins(list: &[&str]) -> Vec<String> {
list.iter().map(|s| s.to_string()).collect()
}
#[test]
fn the_url_is_the_first_origin_without_a_trailing_slash() {
let list = origins(&["https://example.com/", "https://*.example.com", "*"]);
assert_eq!(first_origin(Some(&list)), Some("https://example.com"));
let list = origins(&["http://web.localhost:4000"]);
assert_eq!(first_origin(Some(&list)), Some("http://web.localhost:4000"));
}
#[test]
fn no_component_or_no_origins_is_no_url() {
assert_eq!(first_origin(None), None);
assert_eq!(first_origin(Some(&[])), None);
assert_eq!(first_origin(Some(&origins(&[""]))), None);
}
}