Skip to content

Commit

Permalink
Allow healthcheck to work in host_matching mode
Browse files Browse the repository at this point in the history
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 `<host>` will match any route and pass the
value through as the variable `host` to the view.
  • Loading branch information
samuelhwilliams committed Dec 9, 2024
1 parent b65c285 commit 2d522ab
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions fsd_utils/healthchecks/healthcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="<host>")
self.checkers = []

def healthcheck_view(self):
def healthcheck_view(self, host=None):
responseCode = 200
response = {"checks": []}
version = os.getenv("GITHUB_SHA")
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down
10 changes: 5 additions & 5 deletions tests/test_healthcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="<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="<host>")

expected_dict = {"checks": []}

Expand All @@ -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="<host>")

expected_dict = {"checks": [{"check_a": "ok"}, {"check_b": "ok"}]}

Expand All @@ -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="<host>")

expected_dict = {"checks": [{"check_a": "fail"}, {"check_b": "ok"}]}

Expand All @@ -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="<host>")

expected_dict = {"checks": [{"check_a": "fail"}, {"check_b": "Failed - check logs"}]}

Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2d522ab

Please sign in to comment.