handle static files with tornado

This commit is contained in:
Oskar Hahn 2013-02-18 19:57:46 +01:00
parent 2822c39f8a
commit f388f3d41a
2 changed files with 24 additions and 6 deletions

View File

@ -20,6 +20,8 @@ import tempfile
import threading import threading
import time import time
import webbrowser import webbrowser
import posixpath
from urllib import unquote
from tornado.options import options, define, parse_command_line from tornado.options import options, define, parse_command_line
import tornado.httpserver import tornado.httpserver
@ -309,8 +311,24 @@ def create_or_reset_admin_user():
admin.save() admin.save()
def start_openslides(addr, port, start_browser_url=None, extra_args=[]): class StaticFileHandler(tornado.web.StaticFileHandler):
"""Handels static data by using the django finders."""
def initialize(self):
"""Overwrite some attributes."""
self.root = ''
self.default_filename = None
def get(self, path, include_body=True):
from django.contrib.staticfiles import finders
normalized_path = posixpath.normpath(unquote(path)).lstrip('/')
absolute_path = finders.find(normalized_path)
return super(StaticFileHandler, self).get(absolute_path, include_body)
def start_openslides(addr, port, start_browser_url=None, extra_args=[]):
from django.conf import settings
# Set the options # Set the options
define('port', type=int, default=port) define('port', type=int, default=port)
define('address', default=addr) define('address', default=addr)
@ -321,10 +339,10 @@ def start_openslides(addr, port, start_browser_url=None, extra_args=[]):
# Start the server # Start the server
app = tornado.wsgi.WSGIContainer(django.core.handlers.wsgi.WSGIHandler()) app = tornado.wsgi.WSGIContainer(django.core.handlers.wsgi.WSGIHandler())
tornado_app = tornado.web.Application( tornado_app = tornado.web.Application([
[ (r"%s(.*)" % settings.STATIC_URL, StaticFileHandler),
('.*', tornado.web.FallbackHandler, dict(fallback=app)), ('.*', tornado.web.FallbackHandler, dict(fallback=app))])
])
server = tornado.httpserver.HTTPServer(tornado_app) server = tornado.httpserver.HTTPServer(tornado_app)
server.listen(port=options.port, server.listen(port=options.port,
address=options.address) address=options.address)

View File

@ -5,4 +5,4 @@ PIL==1.1.7
coverage==3.6 coverage==3.6
django-discover-runner==0.2.2 django-discover-runner==0.2.2
pep8==1.4 pep8==1.4
tornado tornado==2.4.1