From 70407feab8af365d884026948b9b2c878c644822 Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Sat, 19 Jan 2013 10:51:42 +0100 Subject: [PATCH 1/5] Use the tornado server to serve the application --- openslides/main.py | 25 ++++++++++++++++++++++--- requirements.txt | 1 + setup.py | 1 + 3 files changed, 24 insertions(+), 3 deletions(-) mode change 100755 => 100644 openslides/main.py diff --git a/openslides/main.py b/openslides/main.py old mode 100755 new mode 100644 index d25305a4d..ad1a12018 --- a/openslides/main.py +++ b/openslides/main.py @@ -21,6 +21,13 @@ import threading import time import webbrowser +from tornado.options import options, define, parse_command_line +import tornado.httpserver +import tornado.ioloop +import tornado.web +import tornado.wsgi + +import django.core.handlers.wsgi import django.conf from django.core.management import execute_from_command_line @@ -291,13 +298,25 @@ def create_or_reset_admin_user(): def start_openslides(addr, port, start_browser_url=None, extra_args=[]): - argv = ["", "runserver", '--noreload'] + extra_args - argv.append("%s:%d" % (addr, port)) + # Set the options + define('port', type=int, default=port) + define('address', default=addr) + # Open the browser if start_browser_url: start_browser(start_browser_url) - execute_from_command_line(argv) + + # Start the server + app = tornado.wsgi.WSGIContainer(django.core.handlers.wsgi.WSGIHandler()) + tornado_app = tornado.web.Application( + [ + ('.*', tornado.web.FallbackHandler, dict(fallback=app)), + ]) + server = tornado.httpserver.HTTPServer(tornado_app) + server.listen(port=options.port, + address=options.address) + tornado.ioloop.IOLoop.instance().start() def start_browser(url): diff --git a/requirements.txt b/requirements.txt index d020ab86f..5dbcc0dda 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ PIL==1.1.7 coverage==3.6 django-discover-runner==0.2.2 pep8==1.4 +tornado diff --git a/setup.py b/setup.py index c6b32c729..1f637afcb 100644 --- a/setup.py +++ b/setup.py @@ -44,6 +44,7 @@ setup( 'django-mptt', 'reportlab', 'pil', + 'tornado', ], entry_points={ 'console_scripts': [ From a14f9b30adc3bc7dbdd251b11b396293a8bdef80 Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Wed, 23 Jan 2013 19:37:12 +0100 Subject: [PATCH 2/5] Fix help text --- openslides/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openslides/main.py b/openslides/main.py index ad1a12018..945cce87b 100644 --- a/openslides/main.py +++ b/openslides/main.py @@ -83,7 +83,7 @@ def process_options(argv=None): argv = sys.argv[1:] parser = optparse.OptionParser( - description="Run openslides using django's builtin webserver") + description="Run openslides using the tornado webserver") parser.add_option("-a", "--address", help="IP Address to listen on.") parser.add_option("-p", "--port", type="int", help="Port to listen on.") parser.add_option( From 3bfead298d5f1816bb4558ead4955b4c152ccd14 Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Wed, 23 Jan 2013 21:12:36 +0100 Subject: [PATCH 3/5] Add custom management command to start the application with tornado --- openslides/main.py | 12 ++++++---- openslides/utils/management/__init__.py | 0 .../utils/management/commands/__init__.py | 0 .../utils/management/commands/run-tornado.py | 23 +++++++++++++++++++ 4 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 openslides/utils/management/__init__.py create mode 100644 openslides/utils/management/commands/__init__.py create mode 100644 openslides/utils/management/commands/run-tornado.py diff --git a/openslides/main.py b/openslides/main.py index 945cce87b..aab5ac203 100644 --- a/openslides/main.py +++ b/openslides/main.py @@ -78,7 +78,7 @@ KEY_LENGTH = 30 _portable_db_path = object() -def process_options(argv=None): +def process_options(argv=None, check_args=True): if argv is None: argv = sys.argv[1:] @@ -105,7 +105,11 @@ def process_options(argv=None): if opts.version: print get_version() exit(0) - if args: + + # Don't check for further args if we come from our custom management + # command, that always sets them + if args and not check_args: + sys.stderr.write("This command does not take arguments!\n\n") parser.print_help() sys.exit(1) @@ -113,8 +117,8 @@ def process_options(argv=None): return opts -def main(argv=None): - opts = process_options(argv) +def main(argv=None, check_args=True): + opts = process_options(argv, check_args) _main(opts) diff --git a/openslides/utils/management/__init__.py b/openslides/utils/management/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/openslides/utils/management/commands/__init__.py b/openslides/utils/management/commands/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/openslides/utils/management/commands/run-tornado.py b/openslides/utils/management/commands/run-tornado.py new file mode 100644 index 000000000..39c5d6361 --- /dev/null +++ b/openslides/utils/management/commands/run-tornado.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +""" + Start script for OpenSlides. + + :copyright: 2011, 2012 by OpenSlides team, see AUTHORS. + :license: GNU GPL, see LICENSE for more details. + +""" + +from django.core.management.base import BaseCommand + +from openslides.main import main + + +class Command(BaseCommand): + ''' + Start the application using the tornado webserver + ''' + + help = 'Start the application using the tornado webserver' + + def handle(self, *args, **options): + main(check_args=False) From bebaadf85082f6542a6604b9bb664ad62492c75e Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Wed, 23 Jan 2013 21:24:02 +0100 Subject: [PATCH 4/5] Actually do the right check for further args --- openslides/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openslides/main.py b/openslides/main.py index aab5ac203..29586bfd7 100644 --- a/openslides/main.py +++ b/openslides/main.py @@ -108,7 +108,7 @@ def process_options(argv=None, check_args=True): # Don't check for further args if we come from our custom management # command, that always sets them - if args and not check_args: + if args and check_args: sys.stderr.write("This command does not take arguments!\n\n") parser.print_help() From f388f3d41a81b2c89b176b0ddba655c9725e9e1a Mon Sep 17 00:00:00 2001 From: Oskar Hahn Date: Mon, 18 Feb 2013 19:57:46 +0100 Subject: [PATCH 5/5] handle static files with tornado --- openslides/main.py | 28 +++++++++++++++++++++++----- requirements.txt | 2 +- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/openslides/main.py b/openslides/main.py index 73d7fa353..427d7151a 100644 --- a/openslides/main.py +++ b/openslides/main.py @@ -20,6 +20,8 @@ import tempfile import threading import time import webbrowser +import posixpath +from urllib import unquote from tornado.options import options, define, parse_command_line import tornado.httpserver @@ -309,8 +311,24 @@ def create_or_reset_admin_user(): 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 define('port', type=int, default=port) define('address', default=addr) @@ -321,10 +339,10 @@ def start_openslides(addr, port, start_browser_url=None, extra_args=[]): # Start the server app = tornado.wsgi.WSGIContainer(django.core.handlers.wsgi.WSGIHandler()) - tornado_app = tornado.web.Application( - [ - ('.*', tornado.web.FallbackHandler, dict(fallback=app)), - ]) + tornado_app = tornado.web.Application([ + (r"%s(.*)" % settings.STATIC_URL, StaticFileHandler), + ('.*', tornado.web.FallbackHandler, dict(fallback=app))]) + server = tornado.httpserver.HTTPServer(tornado_app) server.listen(port=options.port, address=options.address) diff --git a/requirements.txt b/requirements.txt index 5dbcc0dda..3ccabb511 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,4 +5,4 @@ PIL==1.1.7 coverage==3.6 django-discover-runner==0.2.2 pep8==1.4 -tornado +tornado==2.4.1