Repository navigation
Description
FOCA apps need their public URL, e.g., for Location headers of created resources or links in responses, and behind a reverse proxy or ingress it differs from the URL the app sees (scheme, host, port and a path prefix of the proxy). FOCA has no setting for it, so each app adds its own custom config (e.g., custom.endpoints.service with url_prefix, external_host, external_port and api_path) and code. Apps also each write a typed getter for their custom config, as FOCA keeps it as an untyped extra field (get_config().model_extra["custom"]). elixir-cloud-aai/cloud-registry, the template for FOCA apps, has both in cloud_registry/utils.py: get_custom_config(), get_public_url(path) and _format_host() (tests in tests/test_utils.py).
Move them into FOCA, generically:
server.public_url: str | None = None(new): absolutehttp/httpsURL of the app as clients see it, including any path prefix of a proxy, e.g.,https://api.example.org/my-app/. Validated and normalized withfoca.utils.urls(check_url(),normalize_url(); host in URL form, e.g., bracketed IPv6 addresses with encoded zone IDs, so_format_host()is not needed); no query or fragment. The foca-common chart's tests already setcustom.public_urlfromhostName; a chart helper forserver.public_urlis a possible follow-up.foca.utils.urls.get_public_url(path: str = "") -> str: in a request context,public_url, followed by the app's root path (request.script_root) andpath, percent-encoded (the prefix ofpublic_urlas configured), without trailing slash unless it is the host's root, as in cloud-registry. Ifpublic_urlis not set, it raisesRuntimeError, rather than building the URL from the request'sHostandX-Forwarded-*headers, which clients control unless a proxy sets them.foca.get_custom_config(model: type[T]) -> T(infoca.config.access, exported fromfocalikeget_config()): the custom config of the current app, if it is an instance ofmodel; otherwiseRuntimeError, hinting thatcustom_config_modelmust be the model's full module path (a model imported under another module path is another class).- Backwards compatible: new, optional setting and functions; nothing changes for apps that don't use them.
- Docs:
docs/configuration/server.md(public_url, with a proxy example),docs/configuration/custom.md(get_custom_config()),docs/utilities.md(get_public_url()), and the option intemplates/config.yaml.
Follow-up in cloud-registry (separate issue there): replace custom.endpoints.service with server.public_url (a breaking config change there, with its own upgrade notes and chart change), and delete get_custom_config(), get_public_url() and _format_host() from cloud_registry/utils.py.
Open question for the maintainer: should get_public_url() fall back to the request URL (as Flask's url_for(..., _external=True) does) when public_url is not set? This issue raises an error instead, as that is the safe choice and a fallback can be added later.
Acceptance criteria
-
server.public_urlis validated (absolutehttp/httpsURL, no query or fragment) and normalized. -
get_public_url()builds URLs as in cloud-registry's tests (path prefix, root path, percent-encoding, trailing slash, IPv6 hosts) and raises withoutpublic_url. -
get_custom_config()returns the typed custom config and raises for a missing or other model. - Tests at 100% coverage; docs and
templates/config.yamlupdated; the strict docs build passes. - No migration note needed (opt-in); the changelog mentions it.
Activity