Skip to content

Instantly share code, notes, and snippets.

Last active September 24, 2020 17:03
What would you like to do?
Flask on FaaS - minimal example with CGI Handler
  • Place hello.html into templates/

  • Build the Docker image and then deploy

For the time being the FaaS gateway will only accept POST requests, but you can use multiple HTTP methods through the watchdog for testing. A proxy like Nginx could also be used to re-write GET into POST.

FROM python:2.7-alpine
ADD /usr/bin
RUN chmod +x /usr/bin/fwatchdog
WORKDIR /root/
RUN pip install flask
ENV fprocess="python"
HEALTHCHECK --interval=1s CMD [ -e /tmp/.lock ] || exit 1
CMD ["fwatchdog"]
from wsgiref.handlers import CGIHandler
from flask import render_template
from flask import Flask
app = Flask(__name__)
def hello(name=None):
return render_template('hello.html', name=name)
class ProxyFix(object):
def __init__(self, app): = app
def __call__(self, environ, start_response):
environ['SERVER_NAME'] = "localhost"
environ['SERVER_PORT'] = "8080"
environ['REQUEST_METHOD'] = "GET"
environ['SCRIPT_NAME'] = ""
environ['PATH_INFO'] = "/"
environ['QUERY_STRING'] = ""
environ['SERVER_PROTOCOL'] = "HTTP/1.1"
return, start_response)
if __name__ == '__main__':
app.wsgi_app = ProxyFix(app.wsgi_app)
<!doctype html>
<title>Hello from Flask</title>
{% if name %}
<h1>Hello {{ name }}!</h1>
{% else %}
<h1>Hello, World!</h1>
{% endif %}
Copy link

This gist is not supported. You should contact the community via Slack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment