changeset 4261:6de8b4205754

Moved serving-related code into MoinMoin.web.serving module
author Florian Krupicka <florian.krupicka@googlemail.com>
date Sat, 02 Aug 2008 21:37:30 +0200
parents f70003d57e47
children 46ad11b00bed
files MoinMoin/script/server/standalone.py MoinMoin/web/serving.py MoinMoin/wsgiapp.py
diffstat 3 files changed, 52 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/script/server/standalone.py	Sat Aug 02 15:00:48 2008 +0200
+++ b/MoinMoin/script/server/standalone.py	Sat Aug 02 21:37:30 2008 +0200
@@ -13,7 +13,7 @@
 from MoinMoin.script import MoinScript
 from MoinMoin.server.server_standalone import StandaloneConfig
 from MoinMoin.server.daemon import Daemon
-from MoinMoin.wsgiapp import run_server
+from MoinMoin.web.serving import run_server
 
 class PluginScript(MoinScript):
     def __init__(self, argv, def_values):
@@ -118,7 +118,8 @@
                 daemon = Daemon('moin', pidfile, run_server, Config)
                 daemon.do_start()
             else:
-                run_server(Config)
+                run_server(Config.interface, Config.port, Config.docs,
+                           use_debugger=Config.debug)
 
 class DefaultConfig(StandaloneConfig):
     docs = os.path.join('wiki', 'htdocs')
--- a/MoinMoin/web/serving.py	Sat Aug 02 15:00:48 2008 +0200
+++ b/MoinMoin/web/serving.py	Sat Aug 02 21:37:30 2008 +0200
@@ -8,13 +8,19 @@
     @copyright: 2008-2008 MoinMoin:FlorianKrupicka
     @license: GNU GPL, see COPYING for details.
 """
-
-from werkzeug.serving import BaseRequestHandler
+import os
+from MoinMoin import config
+from werkzeug.utils import SharedDataMiddleware
+from werkzeug.serving import BaseRequestHandler, run_simple
 
 from MoinMoin import version, log
 logging = log.getLogger(__name__)
 
 class RequestHandler(BaseRequestHandler):
+    """
+    A request-handler for WSGI, that overrides the default logging
+    mechanisms to log via MoinMoin's logging framework.
+    """
     server_version = "MoinMoin %s %s" % (version.release,
                                          version.revision)
 
@@ -28,3 +34,44 @@
 
     def log_message(self, format, *args):
         logging.info("%s %s", self.address_string(), (format % args))
+
+class ProxyTrust(object):
+    """
+    Middleware that rewrites the remote address according to trusted
+    proxies in the forward chain.
+    """
+
+    def __init__(self, app, proxies):
+        self.app = app
+        self.proxies = proxies
+
+    def __call__(environ, start_response):
+        if 'HTTP_X_FORWARDED_FOR' in environ:
+            addrs = environ.pop('HTTP_X_FORWARDED_FOR').split(',')
+            addrs = [x.strip() for addr in addrs]
+        elif 'REMOTE_ADDR' in environ:
+            addrs = [environ['REMOTE_ADDR']]
+        else:
+            addrs = [None]
+        result = [addr for addr in addrs if addr not in self.proxies]
+        if result:
+            environ['REMOTE_ADDR'] = result[-1]
+        elif addrs[-1] is not None:
+            environ['REMOTE_ADDR'] = addrs[-1]
+        else:
+            del environ['REMOTE_ADDR']
+        return self.app(environ, start_response)
+
+def run_server(host='localhost', port=8080, docs='/usr/share/moin/htdocs',
+               threaded=True, use_debugger=False):
+    """ Run a standalone server on specified host/port. """
+    from MoinMoin.wsgiapp import application
+
+    if docs and os.path.isdir(docs):
+        shared = {url_prefix_static: docs,
+                  '/favicon.ico': os.path.join(docs, 'favicon.ico'),
+                  '/robots.txt': os.path.join(docs, 'robots.txt')}
+        application = SharedDataMiddleware(application, shared)
+
+    run_simple(host, port, application, threaded=threaded,
+               use_debugger=use_debugger)
--- a/MoinMoin/wsgiapp.py	Sat Aug 02 15:00:48 2008 +0200
+++ b/MoinMoin/wsgiapp.py	Sat Aug 02 21:37:30 2008 +0200
@@ -261,44 +261,3 @@
 
     return response(environ, start_response)
 
-class ProxyTrust(object):
-    def __init__(self, app, proxies):
-        self.app = app
-        self.proxies = proxies
-
-    def __call__(environ, start_response):
-        if 'HTTP_X_FORWARDED_FOR' in environ:
-            addrs = environ.pop('HTTP_X_FORWARDED_FOR').split(',')
-            addrs = [x.strip() for addr in addrs]
-        elif 'REMOTE_ADDR' in environ:
-            addrs = [environ['REMOTE_ADDR']]
-        else:
-            addrs = [None]
-        result = [addr for addr in addrs if addr not in self.proxies]
-        if result:
-            environ['REMOTE_ADDR'] = result[-1]
-        elif addrs[-1] is not None:
-            environ['REMOTE_ADDR'] = addrs[-1]
-        else:
-            del environ['REMOTE_ADDR']
-        return self.app(environ, start_response)
-
-def run_server(config):
-    from os import path
-    from MoinMoin.config import url_prefix_static
-    from MoinMoin.web.serving import RequestHandler
-    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.debug
-    params['threaded'] = True
-    params['request_handler'] = RequestHandler
-
-    run_simple(config.interface, config.port, app, **params)