changeset 4270:ab601d804f26

Factor out common frontend code
author Florian Krupicka <florian.krupicka@googlemail.com>
date Wed, 06 Aug 2008 23:21:31 +0200
parents 10ac3ad07faa
children 793bf51b6c76
files MoinMoin/web/cgi_interface.py MoinMoin/web/frontend.py
diffstat 2 files changed, 81 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/web/cgi_interface.py	Wed Aug 06 22:21:08 2008 +0200
+++ b/MoinMoin/web/cgi_interface.py	Wed Aug 06 23:21:31 2008 +0200
@@ -7,53 +7,36 @@
 """
 import optparse
 
-from MoinMoin.web.serving import make_application
+from MoinMoin.web.frontend import ServerFrontEnd
 
 from MoinMoin import log
 logging = log.getLogger(__name__)
 
+class CGIFrontEnd(ServerFrontEnd):
+    def run_server(self, application, options):
+        cgi_fallback = False
+
+        try:
+            from flup.server.fcgi import WSGIServer
+        except ImportError:
+            from MoinMoin.web._fallback_cgi import WSGIServer
+            cgi_fallback = True
+
+        kwargs = {}
+        if options.port:
+            kwargs['bindAddress'] = (options.interface, options.port)
+        elif options.interface.startswith('/') or \
+                options.interface.startswith('./'):
+            kwargs['bindAddress'] = options.interface
+        if not cgi_fallback:
+            WSGIServer(application, **kwargs).run()
+        else:
+            if 'bindAddress' in kwargs:
+                logging.warning('Cannot bind to socket when running with CGI fallback')
+                WSGIServer(application).run()
 
 def run():
-    parser = optparse.OptionParser()
-    parser.add_option("-d", "--debug", action="store_true",
-                      help="Enable debug mode of server (show tracebacks)")
-    parser.add_option("-c", "--config-dir", dest="config_dir", metavar="DIR",
-                      help=("Path to the directory containing the wiki "
-                            "configuration files. Default: current directory"))
-    parser.add_option("-p", "--port", dest="port", type="int",
-                      help="Set the port to listen on. Act as CGI/FCGI script otherwise")
-    parser.add_option("-i", "--interface", dest="interface",
-                      help=("Set the interface/socket to listen on. If starts "
-                            "with '/' or './' it is interpreted as a path "
-                            "to a unix socket. Default: localhost"))
-    parser.set_default('interface', 'localhost')
-
-    cgi_fallback = False
-
-    try:
-        from flup.server.fcgi import WSGIServer
-    except ImportError:
-        from MoinMoin.web._fallback_cgi import WSGIServer
-        cgi_fallback = True
-
-    options, args = parser.parse_args()
-
-    logging.debug('Options: %r', options)
-
-    kwargs = {}
-    if options.port:
-        kwargs['bindAddress'] = (options.interface, options.port)
-    elif options.interface.startswith('/') or \
-            options.interface.startswith('./'):
-        kwargs['bindAddress'] = options.interface
-
-    app = make_application()
-    if not cgi_fallback:
-        WSGIServer(app, **kwargs).run()
-    else:
-        if 'bindAddress' in kwargs:
-            logging.warning('Cannot bind to socket when running with CGI fallback')
-        WSGIServer(app).run()
+    CGIFrontEnd().run()
 
 if __name__ == '__main__':
     run()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/web/frontend.py	Wed Aug 06 23:21:31 2008 +0200
@@ -0,0 +1,57 @@
+# -*- coding: iso-8859-1 -*-
+"""
+    MoinMoin - Common code for frontends (CGI/FCGI/SCGI)
+
+    @copyright: 2008 MoinMoin:FlorianKrupicka
+    @license: GNU GPL, see COPYING for details.
+"""
+import optparse
+
+from MoinMoin.web.serving import make_application
+
+from MoinMoin import log
+logging = log.getLogger(__name__)
+
+class FrontEnd(object):
+    def __init__(self):
+        self.parser = optparse.OptionParser()
+        self.add_options()
+
+    def add_options(self):
+        parser = self.parser
+        parser.add_option("-d", "--debug", action="store_true",
+                          help="Enable debug mode of server (show tracebacks)")
+        parser.add_option("-c", "--config-dir", dest="config_dir", metavar="DIR",
+                          help=("Path to the directory containing the wiki "
+                                "configuration files. Default: current directory"))
+        parser.add_option("--htdocs", dest="htdocs",
+                          help=("Path to the directory containing Moin's "
+                                "static files. Default: /usr/share/moin/htdocs"))
+        parser.set_default('htdocs', '/usr/share/moin/htdocs')
+
+    def run(self, args=None):
+        options, args = self.parser.parse_args(args)
+        logging.debug('Options: %r', options)
+
+        if options.htdocs:
+            application = make_application(shared=options.htdocs)
+        else:
+            application = make_application()
+
+        try:
+            self.run_server(application, options)
+        except:
+            logging.error('Error while running %s', self.__class__.__name__)
+            raise
+
+class ServerFrontEnd(FrontEnd):
+    def add_options(self):
+        super(ServerFrontEnd, self).add_options()
+        parser = self.parser
+        parser.add_option("-p", "--port", dest="port", type="int",
+                          help="Set the port to listen on. Act as CGI/FCGI script otherwise")
+        parser.add_option("-i", "--interface", dest="interface",
+                          help=("Set the interface/socket to listen on. If starts "
+                                "with '/' or './' it is interpreted as a path "
+                                "to a unix socket. Default: localhost"))
+        parser.set_default('interface', 'localhost')