From db3be69d61be375aa3c113a89e075340416ba575 Mon Sep 17 00:00:00 2001 From: Maximilian Moser <maximilian.moser@tuwien.ac.at> Date: Thu, 13 Feb 2025 11:57:53 +0100 Subject: [PATCH] Explicitly set the calculated value for THEME_SITEURL * previously, i forgot to override that configuration item after calculating its value * if not set as configuration explicitly, Invenio-RDM-Records would then set a default value of "127.0.0.1:5000" for it --- invenio_config_tuw/startup/config.py | 24 +++++++++++++++++------- tests/conftest.py | 1 + tests/test_invenio_config_tuw.py | 2 ++ 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/invenio_config_tuw/startup/config.py b/invenio_config_tuw/startup/config.py index 84271b9..2ed303a 100644 --- a/invenio_config_tuw/startup/config.py +++ b/invenio_config_tuw/startup/config.py @@ -177,20 +177,30 @@ def assemble_cache_uri_from_parts(app): def assemble_site_urls_from_parts(app): """Create `LocalProxy` objects for the `SITE_{API,UI}_URL` items.""" - # prefer the `SERVER_NAME` config item to build URLs server_name = _get_config(app, "SERVER_NAME") - theme_siteurl = _get_config( - app, "THEME_SITEURL", default=f"https://{server_name or 'localhost'}" - ) + preferred_scheme = _get_config(app, "PREFERRED_URL_SCHEME", "https") + theme_siteurl = _get_config(app, "THEME_SITEURL") + + # note: the preferred way is setting the `SERVER_NAME` configuration if server_name: - hostname = server_name + theme_siteurl = theme_siteurl or f"{preferred_scheme}://{server_name}" + + elif theme_siteurl: + server_name = theme_siteurl.lstrip("http://").lstrip("https://").split("/")[0] + app.logger.info( + f"No SERVER_NAME set, calculated value '{server_name}' from THEME_SITEURL: '{theme_siteurl}'" + ) + else: - hostname = theme_siteurl.lstrip("http://").lstrip("https://").split("/")[0] + raise RuntimeError( + "Neither SERVER_NAME or THEME_SITEURL are configured. Aborting." + ) # note: 'invenio-cli run' likes to populate INVENIO_SITE_{UI,API}_URL... app.config["SITE_UI_URL"] = LocalProxy(partial(_make_site_url, "")) app.config["SITE_API_URL"] = LocalProxy(partial(_make_site_url, "/api")) - app.config["OAISERVER_ID_PREFIX"] = hostname + app.config["THEME_SITEURL"] = theme_siteurl + app.config["OAISERVER_ID_PREFIX"] = server_name def assemble_keycloak_config_from_parts(app): diff --git a/tests/conftest.py b/tests/conftest.py index 8fb7f7f..331b76e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -120,6 +120,7 @@ def app_config(app_config): app_config["WTF_CSRF_ENABLED"] = False app_config["MAIL_SUPPRESS_SEND"] = True app_config["WEBPACKEXT_MANIFEST_LOADER"] = MockManifestLoader + app_config["SERVER_NAME"] = "localhost" return app_config diff --git a/tests/test_invenio_config_tuw.py b/tests/test_invenio_config_tuw.py index a72a527..687441d 100644 --- a/tests/test_invenio_config_tuw.py +++ b/tests/test_invenio_config_tuw.py @@ -22,10 +22,12 @@ def test_version(): def test_init(): """Test extension initialization.""" app = Flask("testapp") + app.config["SERVER_NAME"] = "localhost" InvenioConfigTUW(app) assert "invenio-config-tuw" in app.extensions app = Flask("testapp") + app.config["SERVER_NAME"] = "localhost" ext = InvenioConfigTUW() assert "invenio-config-tuw" not in app.extensions ext.init_app(app) -- GitLab