Files
romm/backend/utils/urls.py
Georges-Antoine Assi f3e3dc09d4 refactor(config): move public base URL helpers to utils.urls
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-17 08:10:50 -04:00

27 lines
773 B
Python

import ipaddress
import yarl
import config
def is_shareable_host(host: str) -> bool:
"""Whether a host resolves to something other than the instance itself."""
if host.lower() == "localhost":
return False
try:
address = ipaddress.ip_address(host)
except ValueError:
return True # A hostname, so assume it resolves for everyone.
return not address.is_loopback and not address.is_unspecified
def get_public_base_url() -> str | None:
"""Return ROMM_BASE_URL when it points somewhere shareable, else None."""
url = yarl.URL(config.ROMM_BASE_URL)
if url.scheme not in ("http", "https") or not url.host:
return None
if not is_shareable_host(url.host):
return None
return str(url).rstrip("/")