Skip to content

Commit d740eaa

Browse files
authored
Merge pull request #5048 from kobotoolbox/TASK-931-minimal-healthcheck-middleware
[Task 931] Minimal health-check middleware
2 parents 76a82a1 + 078b971 commit d740eaa

File tree

9 files changed

+25
-41
lines changed

9 files changed

+25
-41
lines changed

hub/middleware.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55

66
class LocaleMiddleware(DjangoLocaleMiddleware):
7-
87
def process_response(self, request, response):
98
response = super().process_response(request, response)
109
try:

kobo/apps/openrosa/apps/main/service_health.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,3 @@ def service_health(request):
4747
return HttpResponse(
4848
output, status=(500 if any_failure else 200), content_type='text/plain'
4949
)
50-
51-
def service_health_minimal(request):
52-
return HttpResponse('ok', content_type='text/plain')

kobo/apps/openrosa/apps/main/tests/test_service_health_minimal.py

Lines changed: 0 additions & 13 deletions
This file was deleted.

kobo/apps/openrosa/apps/main/urls.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# coding: utf-8
22
from django.conf import settings
33

4-
from django.urls import include, re_path, path
4+
from django.urls import include, re_path
55
from django.views.generic import RedirectView
66
from django.views.i18n import JavaScriptCatalog
77

@@ -10,10 +10,7 @@
1010
from kobo.apps.openrosa.apps.api.urls import XFormListApi
1111
from kobo.apps.openrosa.apps.api.urls import XFormSubmissionApi
1212
from kobo.apps.openrosa.apps.api.urls import router, router_with_patch_list
13-
from kobo.apps.openrosa.apps.main.service_health import (
14-
service_health,
15-
service_health_minimal,
16-
)
13+
from kobo.apps.openrosa.apps.main.service_health import service_health
1714

1815
# exporting stuff
1916
from kobo.apps.openrosa.apps.viewer.views import (
@@ -38,11 +35,6 @@
3835
re_path('^api/v1/', include(router.urls)),
3936
re_path('^api/v1/', include(router_with_patch_list.urls)),
4037
re_path(r'^legacy/service_health/$', service_health, name='legacy-service-health'),
41-
path(
42-
'legacy/service_health/minimal/',
43-
service_health_minimal,
44-
name='legacy-service-health-minimal',
45-
),
4638

4739
# main website views
4840
re_path(
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from django.http import HttpResponse
2+
3+
4+
class HealthCheckMiddleware:
5+
"""
6+
Provides healthcheck URL that skips other middleware.
7+
Add this before SecurityMiddleware.
8+
https://stackoverflow.com/a/64623669
9+
"""
10+
11+
def __init__(self, get_response):
12+
self.get_response = get_response
13+
14+
def __call__(self, request):
15+
if request.path == '/service_health/minimal/':
16+
return HttpResponse('ok', content_type='text/plain')
17+
return self.get_response(request)

kobo/apps/service_health/test_service_health_minimal.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
from django.test import TestCase
2-
from django.urls import reverse
1+
from django.test import TestCase, override_settings
32

43

54
class ServiceHealthMinimalTestCase(TestCase):
6-
url = reverse('service-health-minimal')
5+
url = '/service_health/minimal/' # Path checked in middleware, can't use reverse
76

7+
@override_settings(ALLOWED_HOSTS=['nope'])
88
def test_service_health_minimal(self):
99
"""
1010
Test for endpoint which makes no connections to databases or other external
11-
services and is helpful for HTTP health checks.
11+
services, ignores ALLOWED_HOSTS, and is helpful for HTTP health checks.
1212
"""
1313
with self.assertNumQueries(0):
1414
res = self.client.get(self.url)

kobo/apps/service_health/urls.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
from django.urls import path
22

3-
from .views import service_health, service_health_minimal
3+
from .views import service_health
44

55
urlpatterns = [
66
path('service_health/', service_health, name='service-health'),
7-
path(
8-
'service_health/minimal/',
9-
service_health_minimal,
10-
name='service-health-minimal',
11-
),
127
]

kobo/apps/service_health/views.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,3 @@ def service_health(request):
113113
return HttpResponse(
114114
output, status=(500 if any_failure else 200), content_type='text/plain'
115115
)
116-
117-
118-
def service_health_minimal(request):
119-
return HttpResponse('ok', content_type='text/plain')

kobo/settings/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147
)
148148

149149
MIDDLEWARE = [
150+
'kobo.apps.service_health.middleware.HealthCheckMiddleware',
150151
'kobo.apps.openrosa.koboform.redirect_middleware.ConditionalRedirects',
151152
'kobo.apps.openrosa.apps.main.middleware.RevisionMiddleware',
152153
'django_dont_vary_on.middleware.RemoveUnneededVaryHeadersMiddleware',

0 commit comments

Comments
 (0)