Last active
July 1, 2020 13:59
-
-
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
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
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