Skip to content

Instantly share code, notes, and snippets.

@chrisguitarguy
Created July 18, 2014 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisguitarguy/5e04c359d631f407ca18 to your computer and use it in GitHub Desktop.
Save chrisguitarguy/5e04c359d631f407ca18 to your computer and use it in GitHub Desktop.
Dev server example for a Flask + Client-Side App
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
We have a little all-api python application that will have a client-side front
end and needed a script to run a dev server. This is it.
The app itself is a flask app, and will be mounted at `/api` in production. This
is an example of how to reproduce a setup like that with Werkzeug and Static.
:copyright: (c) 2014 PMG <http://pmg.co>
:license: MIT
"""
import os.path as path
import sys
import static
from werkzeug.serving import run_simple
from werkzeug.wsgi import DispatcherMiddleware
# CHANGE THIS
from yourapp import app as api
if __name__ == '__main__':
# serve files from the ``public`` directory relative to where this file is
file_app = static.Cling(path.join(path.dirname(path.abspath(__file__)), 'public'))
# DispatcherMiddleware lets you "mount" applications on endpoints, so we'll
# use static as the main as the main application and mount the ``api`` app
# at ``/api``
app = DispatcherMiddleware(file_app, {
'/api': api.wsgi_app, # could just use ``api`` as well
})
# Run it. ``use_reloader`` and ``use_debugger`` produce a similar experience
# to just doing ``api.run(debug=True)``
run_simple('localhost', 5000, app, use_reloader=True, use_debugger=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment