2013-02-27 18:22:24 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
openslides.utils.tornado_webserver
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
:copyright: (c) 2011-2013 by the OpenSlides team, see AUTHORS.
|
|
|
|
:license: GNU GPL, see LICENSE for more details.
|
|
|
|
"""
|
|
|
|
|
2013-03-27 15:53:31 +01:00
|
|
|
import sys
|
2013-02-27 18:22:24 +01:00
|
|
|
import posixpath
|
|
|
|
from urllib import unquote
|
|
|
|
|
|
|
|
from tornado.httpserver import HTTPServer
|
|
|
|
from tornado.ioloop import IOLoop
|
|
|
|
from tornado.web import FallbackHandler, Application, StaticFileHandler
|
|
|
|
from tornado.wsgi import WSGIContainer
|
2013-03-27 15:53:31 +01:00
|
|
|
from tornado.options import options, parse_command_line
|
2013-02-27 18:22:24 +01:00
|
|
|
|
|
|
|
from django.core.handlers.wsgi import WSGIHandler as Django_WSGIHandler
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
|
2013-02-16 16:19:20 +01:00
|
|
|
class DjangoStaticFileHandler(StaticFileHandler):
|
2013-02-27 18:22:24 +01:00
|
|
|
"""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)
|
2013-02-16 16:19:20 +01:00
|
|
|
return super(DjangoStaticFileHandler, self).get(absolute_path, include_body)
|
2013-02-27 18:22:24 +01:00
|
|
|
|
|
|
|
|
2013-03-27 15:53:31 +01:00
|
|
|
def run_tornado(addr, port, reload=False):
|
|
|
|
# Don't try to read the command line args from openslides
|
|
|
|
parse_command_line(args=[])
|
|
|
|
|
|
|
|
# Start the application
|
2013-02-27 18:22:24 +01:00
|
|
|
app = WSGIContainer(Django_WSGIHandler())
|
|
|
|
tornado_app = Application([
|
2013-02-16 16:19:20 +01:00
|
|
|
(r"%s(.*)" % settings.STATIC_URL, DjangoStaticFileHandler),
|
|
|
|
(r'%s(.*)' % settings.MEDIA_URL, StaticFileHandler, {'path': settings.MEDIA_ROOT}),
|
2013-03-27 15:53:31 +01:00
|
|
|
('.*', FallbackHandler, dict(fallback=app))], debug=reload)
|
2013-02-27 18:22:24 +01:00
|
|
|
|
|
|
|
server = HTTPServer(tornado_app)
|
|
|
|
server.listen(port=port,
|
|
|
|
address=addr)
|
|
|
|
IOLoop.instance().start()
|