Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
app = ZappaWSGIMiddleware(simple_app)
# Call with empty WSGI Environment
resp = app(dict(), self._start_response)
print(''.join(resp))
# Pass some unicode through the middleware headers
def simple_app(environ, start_response):
# String of weird characters
status = '301 Moved Permanently'
response_headers = [('Location', 'http://zappa.com/elsewhere' + ugly_string)]
start_response(status, response_headers)
return [ugly_string]
# Wrap the app with the middleware
app = ZappaWSGIMiddleware(simple_app)
# Call with empty WSGI Environment
resp = app(dict(), self._start_response)
print(''.join(resp))
# The application
wsgi_app_function = getattr(self.app_module, self.settings.APP_FUNCTION)
self.trailing_slash = False
else:
try: # Support both for tests
from zappa.ext.django import get_django_wsgi
except ImportError as e: # pragma: no cover
from django_zappa_app import get_django_wsgi
# Get the Django WSGI app from our extension
wsgi_app_function = get_django_wsgi(self.settings.DJANGO_SETTINGS)
self.trailing_slash = True
self.wsgi_app = ZappaWSGIMiddleware(wsgi_app_function)
def lambda_handler(event, context, settings_name="zappa_settings"):
""" An AWS Lambda function which parses specific API Gateway input into a
WSGI request, feeds it to Flask, procceses the Flask response, and returns
that back to the API Gateway.
"""
# Loading settings from a python module
settings = importlib.import_module(settings_name)
# The flask-app module
app_module = importlib.import_module(settings.APP_MODULE)
# The flask-app
app = getattr(app_module, settings.APP_OBJECT)
app.config.from_object('zappa_settings')
app.wsgi_app = ZappaWSGIMiddleware(app.wsgi_app)
# This is a normal HTTP request
if event.get('method', None):
# If we just want to inspect this,
# return this event instead of processing the request
# https://your_api.aws-api.com/?event_echo=true
event_echo = getattr(settings, "EVENT_ECHO", True)
if event_echo:
if 'event_echo' in list(event['params'].values()):
return {'Content': str(event) + '\n' + str(context), 'Status': 200}
# TODO: Enable Let's Encrypt
# # If Let's Encrypt is defined in the settings,
# # and the path is your.domain.com/.well-known/acme-challenge/{{lets_encrypt_challenge_content}},
# # return a 200 of lets_encrypt_challenge_content.
# lets_encrypt_challenge_path = getattr(settings, "LETS_ENCRYPT_CHALLENGE_PATH", None)
# If in DEBUG mode, log all raw incoming events.
if settings.DEBUG:
logger.info('Zappa Event: {}'.format(event))
# This is a normal HTTP request
if event.get('method', None):
# Create the environment for WSGI and handle the request
environ = create_wsgi_request(event, script_name=settings.SCRIPT_NAME)
# We are always on https on Lambda, so tell our wsgi app that.
environ['HTTPS'] = 'on'
environ['wsgi.url_scheme'] = 'https'
wrap_me = get_wsgi_application()
app = ZappaWSGIMiddleware(wrap_me)
# Execute the application
response = Response.from_app(app, environ)
response.content = response.data
# Prepare the special dictionary which will be returned to the API GW.
returnme = {'Content': response.data}
# Pack the WSGI response into our special dictionary.
for (header_name, header_value) in response.headers:
returnme[header_name] = header_value
returnme['Status'] = response.status_code
# To ensure correct status codes, we need to
# pack the response as a deterministic B64 string and raise it
# as an error to match our APIGW regex.