Skip to content

Commit 6171dbf

Browse files
authored
Merge pull request #7948 from readthedocs/proxito-health-check
Add proxito healthcheck
2 parents 9af02e6 + 31a1dc3 commit 6171dbf

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

readthedocs/core/views/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from django.conf import settings
1111
from django.http import Http404, JsonResponse
1212
from django.shortcuts import render, get_object_or_404, redirect
13-
from django.views.generic import TemplateView
13+
from django.views.generic import View, TemplateView
1414

1515
from readthedocs.builds.models import Version
1616
from readthedocs.core.utils.general import wipe_version_via_slugs
@@ -23,6 +23,11 @@ class NoProjectException(Exception):
2323
pass
2424

2525

26+
class HealthCheckView(View):
27+
def get(self, request, *args, **kwargs):
28+
return JsonResponse({'status': 200}, status=200)
29+
30+
2631
class HomepageView(TemplateView):
2732

2833
template_name = 'homepage.html'

readthedocs/proxito/middleware.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from django.conf import settings
1212
from django.shortcuts import render
1313
from django.utils.deprecation import MiddlewareMixin
14+
from django.urls import reverse
1415

1516
from readthedocs.projects.models import Domain, Project
1617

@@ -157,8 +158,12 @@ def add_proxito_headers(self, request, response):
157158
response['X-RTD-Version-Method'] = 'path'
158159

159160
def process_request(self, request): # noqa
160-
if any([not settings.USE_SUBDOMAIN, 'localhost' in request.get_host(),
161-
'testserver' in request.get_host()]):
161+
if any([
162+
not settings.USE_SUBDOMAIN,
163+
'localhost' in request.get_host(),
164+
'testserver' in request.get_host(),
165+
request.path.startswith(reverse('health_check')),
166+
]):
162167
log.debug('Not processing Proxito middleware')
163168
return None
164169

readthedocs/proxito/tests/test_full.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@
3535
class TestFullDocServing(BaseDocServing):
3636
# Test the full range of possible doc URL's
3737

38+
def test_health_check(self):
39+
url = reverse('health_check')
40+
host = 'project.dev.readthedocs.io'
41+
resp = self.client.get(url, HTTP_HOST=host)
42+
self.assertEqual(resp.status_code, 200)
43+
self.assertEqual(resp.json(), {'status': 200})
44+
45+
# Test with IP address, which should still work
46+
# since we're skipping middleware
47+
host = '127.0.0.1'
48+
resp = self.client.get(url, HTTP_HOST=host)
49+
self.assertEqual(resp.status_code, 200)
50+
self.assertEqual(resp.json(), {'status': 200})
51+
3852
def test_subproject_serving(self):
3953
url = '/projects/subproject/en/latest/awesome.html'
4054
host = 'project.dev.readthedocs.io'

readthedocs/proxito/urls.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from django.views import defaults
3939

4040
from readthedocs.constants import pattern_opts
41+
from readthedocs.core.views import HealthCheckView
4142
from readthedocs.projects.views.public import ProjectDownloadMedia
4243
from readthedocs.proxito.views.serve import (
4344
ServeDocs,
@@ -50,6 +51,13 @@
5051

5152
DOC_PATH_PREFIX = getattr(settings, 'DOC_PATH_PREFIX', '')
5253

54+
health_check_urls = [
55+
url('^{DOC_PATH_PREFIX}health_check/$'.format(DOC_PATH_PREFIX=DOC_PATH_PREFIX),
56+
HealthCheckView.as_view(),
57+
name='health_check',
58+
),
59+
]
60+
5361
proxied_urls = [
5462
# Serve project downloads
5563
# /_/downloads/<lang>/<ver>/<type>/
@@ -163,7 +171,7 @@
163171
),
164172
]
165173

166-
urlpatterns = proxied_urls + core_urls + docs_urls
174+
urlpatterns = health_check_urls + proxied_urls + core_urls + docs_urls
167175

168176
# Use Django default error handlers to make things simpler
169177
handler404 = proxito_404_page_handler

0 commit comments

Comments
 (0)