changeset 4228:d9b3c6585585

First implementation of standalone server using werkzeug.serving.run_simple
author Florian Krupicka <florian.krupicka@googlemail.com>
date Fri, 18 Jul 2008 23:08:48 +0200
parents b459b036f263
children c60dee8f45be
files MoinMoin/script/server/standalone.py MoinMoin/wsgiapp.py
diffstat 2 files changed, 37 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/script/server/standalone.py	Fri Jul 18 01:28:31 2008 +0200
+++ b/MoinMoin/script/server/standalone.py	Fri Jul 18 23:08:48 2008 +0200
@@ -11,8 +11,9 @@
 import signal
 
 from MoinMoin.script import MoinScript
-from MoinMoin.server.server_standalone import StandaloneConfig, run
+from MoinMoin.server.server_standalone import StandaloneConfig
 from MoinMoin.server.daemon import Daemon
+from MoinMoin.wsgiapp import run_server
 
 class PluginScript(MoinScript):
     def __init__(self, argv, def_values):
@@ -38,18 +39,6 @@
             help="Set the ip to listen on. Use \"\" for all interfaces. Default: localhost"
         )
         self.parser.add_option(
-            "--serverClass", dest="serverClass",
-            help="Set the server model to use. Choices: ThreadPool, serverClass, Forking, Simple. Default: ThreadPool"
-        )
-        self.parser.add_option(
-            "--threadLimit", dest="threadLimit", type="int",
-            help="Set the maximum number of threads to use. Default: 10"
-        )
-        self.parser.add_option(
-            "--requestQueueSize", dest="requestQueueSize", type="int",
-            help="Set the size of the request queue. Default: 50"
-        )
-        self.parser.add_option(
             "--start", dest="start", action="store_true",
             help="Start server in background."
         )
@@ -61,23 +50,16 @@
             "--pidfile", dest="pidfile",
             help="Set file to store pid of moin daemon in. Default: moin.pid"
         )
+        self.parser.add_option(
+            "--reload", dest="reload", action="store_true",
+            help="Reload the server if there are changes to any loaded python files"
+         )
 
     def mainloop(self):
         # we don't expect non-option arguments
         if self.args:
             self.parser.error("incorrect number of arguments")
 
-        thread_choices = ["ThreadPool", "Threading", "Forking", "Simple"]
-        serverClass = "ThreadPool"
-        if self.options.serverClass:
-            thread_choices2 = [x.upper() for x in thread_choices]
-            thread_choice = self.options.serverClass.upper()
-            try:
-                serverClass_index = thread_choices2.index(thread_choice)
-            except ValueError:
-                self.parser.error("invalid serverClass type")
-            serverClass = thread_choices[serverClass_index]
-
         pidfile = "moin.pid"
         if self.options.pidfile:
             pidfile = self.options.pidfile
@@ -97,7 +79,7 @@
             try:
                 if self.options.config_dir:
                     sys.path.insert(0, self.options.config_dir)
-                from wikiserverconfig import Config
+                from wikiconfig import Config
             except ImportError, err:
                 if 'Config' in str(err):
                     # we are unable to import Config
@@ -114,17 +96,20 @@
                 Config.port = self.options.port
             if self.options.interface:
                 Config.interface = self.options.interface
-            Config.serverClass = serverClass + 'Server'
-            if self.options.threadLimit:
-                Config.threadLimit = self.options.threadLimit
-            if self.options.requestQueueSize:
-                Config.requestQueueSize = self.options.requestQueueSize
+
+            if not hasattr(Config, 'docs'):
+                docs = os.path.join('wiki', 'htdocs')
+                if not os.path.exists(docs):
+                    docs = "/usr/share/moin/htdocs"
+                Config.docs = docs
+
+            Config.reload_server = self.options.reload
 
             if self.options.start:
-                daemon = Daemon('moin', pidfile, run, Config)
+                daemon = Daemon('moin', pidfile, run_server, Config)
                 daemon.do_start()
             else:
-                run(Config)
+                run_server(Config)
 
 class DefaultConfig(StandaloneConfig):
     docs = os.path.join('wiki', 'htdocs')
@@ -134,6 +119,3 @@
     group = ''
     port = 8080
     interface = 'localhost'
-    serverClass = 'ThreadPoolServer'
-    threadLimit = 10
-    requestQueueSize = 50
--- a/MoinMoin/wsgiapp.py	Fri Jul 18 01:28:31 2008 +0200
+++ b/MoinMoin/wsgiapp.py	Fri Jul 18 23:08:48 2008 +0200
@@ -169,7 +169,7 @@
             # Try action
             else:
                 from MoinMoin import action
-                handler = action.getHandler(request, action_name)
+                handler = action.getHandler(request.cfg, action_name)
                 if handler is None:
                     msg = _("You are not allowed to do %(action_name)s on this page.") % {
                             'action_name': wikiutil.escape(action_name), }
@@ -206,3 +206,22 @@
 
 application = Request.application(application)
 application = HTTPExceptionsMiddleware(application)
+
+def run_server(config):
+    from os import path
+    from MoinMoin.config import url_prefix_static
+    from werkzeug.serving import run_simple
+    from werkzeug.utils import SharedDataMiddleware
+
+    shared = {url_prefix_static: config.docs,
+              '/favicon.ico': path.join(config.docs, 'favicon.ico'),
+              '/robots.txt': path.join(config.docs, 'robots.txt')}
+
+    app = SharedDataMiddleware(application, shared)
+
+    params = {}
+    params['use_debugger'] = config.traceback_show
+    params['threaded'] = True
+    params['use_reloader'] = config.reload_server
+
+    run_simple(config.interface, config.port, app, **params)