1
0
mirror of https://github.com/djohnlewis/stackdump synced 2024-12-04 23:17:37 +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

View File

@ -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
}

View File

@ -1,9 +1,20 @@
from bottle import route, run
from bottle import route, run, static_file
import servers
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
# 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')
def hello():
return "Hello World!"
@ -12,23 +23,29 @@ def hello():
# INITIALISATION
# load the settings file
__import__('settings')
if 'settings' in sys.modules.keys():
settings = sys.modules.get('settings')
settings = dict([ (k, getattr(settings, k)) for k in dir(settings) if not k.startswith('__') ])
else:
settings = { }
# run the server!
server = settings.get('SERVER_ADAPTER', 'wsgiref')
# look for definitions in server, otherwise let bottle decide
server = servers.definitions.get(server, server)
run(
server=server,
host=settings.get('SERVER_HOST', '0.0.0.0'),
port=settings.get('SERVER_PORT', 8080)
)
if __name__ == '__main__':
# only print this on the parent process, not the child ones. Applies when
# the auto-reload option is on (reloader=True). When it is on, the
# BOTTLE_CHILD env var is True if this is the child process.
if not os.environ.get('BOTTLE_CHILD', True):
print('Serving media from: %s' % MEDIA_ROOT)
# load the settings file
__import__('settings')
if 'settings' in sys.modules.keys():
settings = sys.modules.get('settings')
settings = dict([ (k, getattr(settings, k)) for k in dir(settings) if not k.startswith('__') ])
else:
settings = { }
# run the server!
server = settings.get('SERVER_ADAPTER', 'wsgiref')
run(
server=server,
host=settings.get('SERVER_HOST', '0.0.0.0'),
port=settings.get('SERVER_PORT', 8080),
reloader=True
)
# END INITIALISATION