Skip to content

Instantly share code, notes, and snippets.

@mariocesar

mariocesar/wsgi.py

Last active Jul 1, 2020
Embed
What would you like to do?
Adding custom header with the deploy datetime of a Django app. Useful also for adding revision, and other deploy information
import os
import subprocess
from datetime import datetime
import django
from django.core.handlers.wsgi import WSGIHandler
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "conf.settings.develop")
DEPLOY_REVISION = subprocess.check_output(["git", "rev-parse", "HEAD"]).decode().strip()
class Application(WSGIHandler):
def __init__(self):
deploy_datetime = datetime.utcnow().isoformat()
self.served_by = f"revision={DEPLOY_REVISION}&date={deploy_datetime}"
super().__init__()
def get_response(self, request):
setattr(django, "_wsgi_get_response", True) # noqa: B010
response = super().get_response(request)
response["X-Served-By"] = self.served_by
return response
def get_wsgi_application():
django.setup(set_prefix=False)
return Application()
application = get_wsgi_application()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment