From 2d522ab24f0f6bb39081d7bae0aa895dbf338935 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Mon, 9 Dec 2024 14:55:16 +0000 Subject: [PATCH] Allow healthcheck to work in host_matching mode When a Flask app is running in `host_matching` mode, it will only route inbound requests to registered endpoints that set a `host` parameter on the route. In order to register a view against all hosts, you can use route variables like normal, so `` will match any route and pass the value through as the variable `host` to the view. --- CHANGELOG.md | 4 ++++ fsd_utils/healthchecks/healthcheck.py | 4 ++-- pyproject.toml | 2 +- tests/test_healthcheck.py | 10 +++++----- uv.lock | 2 +- 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fd9ad00..5c1a3ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### 5.3.0 + +* Update the Healthcheck to support apps running in `host_matching` mode. + ### 5.2.0 * Adding check internal user functionality and adding FAB and Form Designer to list of supported apps diff --git a/fsd_utils/healthchecks/healthcheck.py b/fsd_utils/healthchecks/healthcheck.py index 1329673..87c1505 100644 --- a/fsd_utils/healthchecks/healthcheck.py +++ b/fsd_utils/healthchecks/healthcheck.py @@ -6,10 +6,10 @@ class Healthcheck(object): def __init__(self, app): self.flask_app = app - self.flask_app.add_url_rule("/healthcheck", view_func=self.healthcheck_view) + self.flask_app.add_url_rule("/healthcheck", view_func=self.healthcheck_view, host="") self.checkers = [] - def healthcheck_view(self): + def healthcheck_view(self, host=None): responseCode = 200 response = {"checks": []} version = os.getenv("GITHUB_SHA") diff --git a/pyproject.toml b/pyproject.toml index 768092a..1956d0a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta" [project] name = "funding-service-design-utils" -version = "5.2.0" +version = "5.3.0" authors = [ { name="MHCLG", email="FundingService@communities.gov.uk" }, diff --git a/tests/test_healthcheck.py b/tests/test_healthcheck.py index 16beab3..841af9a 100644 --- a/tests/test_healthcheck.py +++ b/tests/test_healthcheck.py @@ -10,14 +10,14 @@ class TestHealthcheck: def testHealthChecksSetup(self): test_app = Mock() health = Healthcheck(test_app) - test_app.add_url_rule.assert_called_with("/healthcheck", view_func=ANY) + test_app.add_url_rule.assert_called_with("/healthcheck", view_func=ANY, host="") assert health.checkers == [], "Checks not initialised" @mock.patch.dict(os.environ, clear=True) def testWithNoChecks(self): mock_app = Mock() health = Healthcheck(mock_app) - mock_app.add_url_rule.assert_called_with("/healthcheck", view_func=ANY) + mock_app.add_url_rule.assert_called_with("/healthcheck", view_func=ANY, host="") expected_dict = {"checks": []} @@ -29,7 +29,7 @@ def testWithNoChecks(self): def testWithChecksPassing_mocks(self, flask_test_client): test_app = Mock() health = Healthcheck(test_app) - test_app.add_url_rule.assert_called_with("/healthcheck", view_func=ANY) + test_app.add_url_rule.assert_called_with("/healthcheck", view_func=ANY, host="") expected_dict = {"checks": [{"check_a": "ok"}, {"check_b": "ok"}]} @@ -50,7 +50,7 @@ def testWithChecksPassing_mocks(self, flask_test_client): def testWithChecksFailing_mocks(self, flask_test_client): test_app = Mock() health = Healthcheck(test_app) - test_app.add_url_rule.assert_called_with("/healthcheck", view_func=ANY) + test_app.add_url_rule.assert_called_with("/healthcheck", view_func=ANY, host="") expected_dict = {"checks": [{"check_a": "fail"}, {"check_b": "ok"}]} @@ -71,7 +71,7 @@ def testWithChecksFailing_mocks(self, flask_test_client): def testWithChecksException_mocks(self, flask_test_client): test_app = Mock() health = Healthcheck(test_app) - test_app.add_url_rule.assert_called_with("/healthcheck", view_func=ANY) + test_app.add_url_rule.assert_called_with("/healthcheck", view_func=ANY, host="") expected_dict = {"checks": [{"check_a": "fail"}, {"check_b": "Failed - check logs"}]} diff --git a/uv.lock b/uv.lock index 2eddcd1..6d7d1ac 100644 --- a/uv.lock +++ b/uv.lock @@ -406,7 +406,7 @@ wheels = [ [[package]] name = "funding-service-design-utils" -version = "5.2.0" +version = "5.3.0" source = { editable = "." } dependencies = [ { name = "beautifulsoup4" },