Last active
September 5, 2017 22:14
-
-
Save alfredodeza/5911893 to your computer and use it in GitHub Desktop.
Serve a WSGI application + static files with CherryPy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import cherrypy | |
from cherrypy import wsgiserver | |
from my_wsgi_app import wsgi | |
PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), 'public')) | |
class Root(object): | |
pass | |
def make_static_config(static_dir_name): | |
""" | |
All custom static configurations are set here, since most are common, it | |
makes sense to generate them just once. | |
""" | |
static_path = os.path.join('/', static_dir_name) | |
path = os.path.join(PATH, static_dir_name) | |
configuration = {static_path: { | |
'tools.staticdir.on': True, | |
'tools.staticdir.dir': path} | |
} | |
print configuration | |
return cherrypy.tree.mount(Root(), '/', config=configuration) | |
# Assuming your app has media on diferent paths, like 'c', 'i' and 'j' | |
application = wsgiserver.WSGIPathInfoDispatcher({ | |
'/': wsgi.application, | |
'/c': make_static_config('c'), | |
'/j': make_static_config('j'), | |
'/i': make_static_config('i')}) | |
server = wsgiserver.CherryPyWSGIServer(('0.0.0.0', 8070), application, | |
server_name='www.cherrypy.example') | |
try: | |
server.start() | |
except KeyboardInterrupt: | |
print "Terminating server..." | |
server.stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment