1
0
mirror of https://github.com/djohnlewis/stackdump synced 2025-04-07 10:13:27 +00:00

Added static files serving function, and cleaned up more Jython hacks.

This commit is contained in:
Samuel Lai 2011-10-23 18:17:23 +11:00
parent 810e2e5fe3
commit bc1572be52
2 changed files with 36 additions and 50 deletions

@ -1,31 +0,0 @@
from bottle import ServerAdapter
import sys
class CherryPyServer(ServerAdapter):
'''
This copy of bottle's CherryPyServer is necessary so we can set the nodelay
option to false when running under Jython. Otherwise it will error out as
the TCP_NODELAY option is not supported with Jython.
'''
def run(self, handler): # pragma: no cover
from cherrypy import wsgiserver
server = wsgiserver.CherryPyWSGIServer((self.host, self.port), handler)
# Jython doesn't work with the TCP_NODELAY option
if sys.platform.startswith('java'):
server.nodelay = False
try:
server.start()
finally:
server.stop()
# in order for these to be specified in settings.py, they need to be in the
# following dictionary.
#
# if the name clashes with the default bottle ones, they definition here will
# be used instead.
definitions = {
'cherrypy' : CherryPyServer
}

@ -1,9 +1,20 @@
from bottle import route, run from bottle import route, run, static_file
import servers import servers
import sys import sys
import os
# STATIC VARIABLES
BOTTLE_ROOT = os.path.abspath(os.path.dirname(sys.argv[0]))
MEDIA_ROOT = os.path.abspath(BOTTLE_ROOT + '/../media')
# WEB REQUEST METHODS # WEB REQUEST METHODS
# Bottle will protect us against nefarious peeps using ../ hacks.
@route('/media/:filename#.*#')
def serve_static(filename):
return static_file(filename, root=MEDIA_ROOT)
@route('/hello') @route('/hello')
def hello(): def hello():
return "Hello World!" return "Hello World!"
@ -12,23 +23,29 @@ def hello():
# INITIALISATION # INITIALISATION
# load the settings file if __name__ == '__main__':
__import__('settings') # only print this on the parent process, not the child ones. Applies when
if 'settings' in sys.modules.keys(): # the auto-reload option is on (reloader=True). When it is on, the
settings = sys.modules.get('settings') # BOTTLE_CHILD env var is True if this is the child process.
settings = dict([ (k, getattr(settings, k)) for k in dir(settings) if not k.startswith('__') ]) if not os.environ.get('BOTTLE_CHILD', True):
else: print('Serving media from: %s' % MEDIA_ROOT)
settings = { }
# run the server! # load the settings file
server = settings.get('SERVER_ADAPTER', 'wsgiref') __import__('settings')
# look for definitions in server, otherwise let bottle decide if 'settings' in sys.modules.keys():
server = servers.definitions.get(server, server) settings = sys.modules.get('settings')
settings = dict([ (k, getattr(settings, k)) for k in dir(settings) if not k.startswith('__') ])
else:
settings = { }
run( # run the server!
server=server, server = settings.get('SERVER_ADAPTER', 'wsgiref')
host=settings.get('SERVER_HOST', '0.0.0.0'),
port=settings.get('SERVER_PORT', 8080) run(
) server=server,
host=settings.get('SERVER_HOST', '0.0.0.0'),
port=settings.get('SERVER_PORT', 8080),
reloader=True
)
# END INITIALISATION # END INITIALISATION