Skip to content

Instantly share code, notes, and snippets.

@mariocesar
Last active July 1, 2020 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariocesar/b5a8a4190d44d8d056d12415e36fdc01 to your computer and use it in GitHub Desktop.
Save mariocesar/b5a8a4190d44d8d056d12415e36fdc01 to your computer and use it in GitHub Desktop.
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