Last active
May 26, 2017 01:04
-
-
Save holybit/e2cafba405f9ac6786a09e73df67695e to your computer and use it in GitHub Desktop.
Django custom module endpoint URL problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Django v1.7.7 - no I can't upgrade due to factors beyond my control | |
# This file, middleware.py, is in a Django app directory and correctly added to MIDDLEWARE_CLASSES. | |
# Currently, I get a 404 when tring to access localhost:8000/liveness/ | |
# Howver, I can't figure out what the urlpatterns should be for the | |
# the liveness/ and readiness/ endpoints. | |
import logging | |
from django.http import HttpResponse, HttpResponseServerError | |
logger = logging.getLogger("liveness") | |
class HealthCheckMiddleware(object): | |
def liveness(self, request): | |
if request.method == "GET": | |
if request.path == "/liveness": | |
return HttpResponse("OK") | |
def readiness(self, request): | |
# Connect to each database and do a generic standard SQL query | |
# that doesn't write any data and doesn't depend on any tables | |
# being present. | |
if request.path == "/readiness": | |
try: | |
from django.db import connections | |
for name in connections: | |
cursor = connections[name].cursor() | |
cursor.execute("SELECT 1;") | |
row = cursor.fetchone() | |
if row is None: | |
return HttpResponseServerError("db: invalid response") | |
except Exception, e: | |
logger.exception(e) | |
return HttpResponseServerError("db: cannot connect to database.") | |
return HttpResponse("OK") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment