2018-11-11 12:41:48 +00:00
|
|
|
"""
|
|
|
|
WSGI config for passbook project.
|
|
|
|
|
|
|
|
It exposes the WSGI callable as a module-level variable named ``application``.
|
|
|
|
|
|
|
|
For more information on this file, see
|
|
|
|
https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/
|
|
|
|
"""
|
|
|
|
import os
|
2019-10-04 10:44:59 +00:00
|
|
|
from time import time
|
2018-11-11 12:41:48 +00:00
|
|
|
|
2020-02-21 08:00:28 +00:00
|
|
|
from defusedxml import defuse_stdlib
|
2018-11-11 12:41:48 +00:00
|
|
|
from django.core.wsgi import get_wsgi_application
|
2019-10-04 10:44:59 +00:00
|
|
|
from structlog import get_logger
|
|
|
|
|
2019-12-05 13:33:55 +00:00
|
|
|
from passbook.lib.utils.http import _get_client_ip_from_meta
|
|
|
|
|
2019-10-04 10:44:59 +00:00
|
|
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "passbook.root.settings")
|
2020-02-21 08:00:28 +00:00
|
|
|
defuse_stdlib()
|
2019-10-04 10:44:59 +00:00
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
|
2019-10-04 10:44:59 +00:00
|
|
|
class WSGILogger:
|
|
|
|
""" This is the generalized WSGI middleware for any style request logging. """
|
|
|
|
|
2020-08-19 08:32:44 +00:00
|
|
|
def __init__(self, _application):
|
|
|
|
self.application = _application
|
2019-12-31 11:51:16 +00:00
|
|
|
self.logger = get_logger("passbook.wsgi")
|
2019-10-04 10:44:59 +00:00
|
|
|
|
|
|
|
def __healthcheck(self, start_response):
|
2019-12-31 11:51:16 +00:00
|
|
|
start_response("204 OK", [])
|
|
|
|
return [b""]
|
2019-10-04 10:44:59 +00:00
|
|
|
|
|
|
|
def __call__(self, environ, start_response):
|
|
|
|
start = time()
|
|
|
|
status_codes = []
|
|
|
|
content_lengths = []
|
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
if environ.get("HTTP_HOST", "").startswith("kubernetes-healthcheck-host"):
|
2019-10-04 10:44:59 +00:00
|
|
|
# Don't log kubernetes health/readiness requests
|
|
|
|
return self.__healthcheck(start_response)
|
|
|
|
|
|
|
|
def custom_start_response(status, response_headers, exc_info=None):
|
2019-12-31 11:51:16 +00:00
|
|
|
status_codes.append(int(status.partition(" ")[0]))
|
2019-10-04 10:44:59 +00:00
|
|
|
for name, value in response_headers:
|
2019-12-31 11:51:16 +00:00
|
|
|
if name.lower() == "content-length":
|
2019-10-04 10:44:59 +00:00
|
|
|
content_lengths.append(int(value))
|
|
|
|
break
|
|
|
|
return start_response(status, response_headers, exc_info)
|
2019-12-31 11:51:16 +00:00
|
|
|
|
2019-10-04 10:44:59 +00:00
|
|
|
retval = self.application(environ, custom_start_response)
|
2019-12-31 11:51:16 +00:00
|
|
|
runtime = int((time() - start) * 10 ** 6)
|
2019-10-04 10:44:59 +00:00
|
|
|
content_length = content_lengths[0] if content_lengths else 0
|
2019-12-05 13:33:55 +00:00
|
|
|
self.log(status_codes[0], environ, content_length, runtime=runtime)
|
2019-10-04 10:44:59 +00:00
|
|
|
return retval
|
|
|
|
|
|
|
|
def log(self, status_code, environ, content_length, **kwargs):
|
|
|
|
"""
|
|
|
|
Apache log format 'NCSA extended/combined log':
|
|
|
|
"%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\""
|
|
|
|
see http://httpd.apache.org/docs/current/mod/mod_log_config.html#formats
|
|
|
|
"""
|
2019-12-05 13:33:55 +00:00
|
|
|
host = _get_client_ip_from_meta(environ)
|
2019-12-31 11:51:16 +00:00
|
|
|
query_string = ""
|
|
|
|
if environ.get("QUERY_STRING") != "":
|
2019-10-04 10:44:59 +00:00
|
|
|
query_string = f"?{environ.get('QUERY_STRING')}"
|
2019-12-31 11:51:16 +00:00
|
|
|
self.logger.info(
|
2020-02-16 13:36:31 +00:00
|
|
|
"request",
|
|
|
|
path=f"{environ.get('PATH_INFO', '')}{query_string}",
|
2019-12-31 11:51:16 +00:00
|
|
|
host=host,
|
|
|
|
method=environ.get("REQUEST_METHOD", ""),
|
|
|
|
protocol=environ.get("SERVER_PROTOCOL", ""),
|
|
|
|
status=status_code,
|
|
|
|
size=content_length / 1000 if content_length > 0 else "-",
|
|
|
|
runtime=kwargs.get("runtime"),
|
|
|
|
)
|
2018-11-11 12:41:48 +00:00
|
|
|
|
|
|
|
|
2019-10-04 10:44:59 +00:00
|
|
|
application = WSGILogger(get_wsgi_application())
|