def serve_static(path: Path, statics: Statics, environ: wsgi.WSGIEnviron) -> wsgi.WSGIResponse: if not path.startswith('/'): path = Path('/' + path) static_file = statics.whitenoise.files.get(path) if static_file is None: raise exceptions.NotFound() response = static_file.get_response(environ['REQUEST_METHOD'], environ) status_line = '{} {}'.format(response.status, response.status.phrase) headers = list(response.headers) if response.file is not None: file_wrapper = environ.get('wsgi.file_wrapper', FileWrapper) content = file_wrapper(response.file) else: # We hit this branch for HEAD requests content = [] return wsgi.WSGIResponse(status_line, headers, content)
def json_view() -> wsgi.WSGIResponse: content = json.dumps({'message': 'Hello, world!'}).encode('utf-8') return wsgi.WSGIResponse('200 OK', [('Content-Type', 'application/json'), ('Content-Length', str(len(content)))], [content])
def plaintext_view() -> wsgi.WSGIResponse: content = b'Hello, world!' return wsgi.WSGIResponse('200 OK', [('Content-Type', 'text/plain'), ('Content-Length', str(len(content)))], [content])
def get_wsgi_response() -> wsgi.WSGIResponse: return wsgi.WSGIResponse('200 OK', [('Content-Type', 'application/json')], [b'{"hello": "world"}'])