[wsgi] add the simplest possible wsgi (debug) server
authorAurelien Campeas
Tue, 01 Oct 2013 16:45:58 +0200
changeset 9287 e70c8c70e344
parent 9286 02a491f6fa92
child 9288 823cf14bcc37
[wsgi] add the simplest possible wsgi (debug) server This server is able to: * serve on a given port using the stdlib SimpleHTTPServer * run looping tasks Closes #3005509.
cwctl.py
wsgi/handler.py
wsgi/server.py
--- a/cwctl.py	Thu Oct 10 13:46:44 2013 +0200
+++ b/cwctl.py	Tue Oct 01 16:45:58 2013 +0200
@@ -1,4 +1,4 @@
-# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
 # This file is part of CubicWeb.
@@ -1033,9 +1033,38 @@
                     raise ConfigurationError('unknown configuration key "%s" for mode %s' % (key, appcfg.name))
             appcfg.save()
 
+
+# WSGI #########
+
+class WSGIDebugStartHandler(InstanceCommand):
+    """Start an interactive wsgi server """
+    name = 'wsgi'
+    actionverb = 'started'
+    arguments = '<instance>'
+    options = (
+        ('loglevel',
+         {'short': 'l',
+          'type' : 'choice',
+          'metavar': '<log level>',
+          'default': 'debug',
+          'choices': ('debug', 'info', 'warning', 'error'),
+          'help': 'debug if -D is set, error otherwise',
+          }),
+        )
+
+    def wsgi_instance(self, appid):
+        config = cwcfg.config_for(appid, debugmode=1)
+        init_cmdline_log_threshold(config, self['loglevel'])
+        assert config.name == 'all-in-one'
+        from cubicweb.wsgi import server
+        return server.run(config)
+
+
+
 for cmdcls in (ListCommand,
                CreateInstanceCommand, DeleteInstanceCommand,
                StartInstanceCommand, StopInstanceCommand, RestartInstanceCommand,
+               WSGIDebugStartHandler,
                ReloadConfigurationCommand, StatusCommand,
                UpgradeInstanceCommand,
                ListVersionsInstanceCommand,
@@ -1046,6 +1075,8 @@
                ):
     CWCTL.register(cmdcls)
 
+
+
 def run(args):
     """command line tool"""
     import os
--- a/wsgi/handler.py	Thu Oct 10 13:46:44 2013 +0200
+++ b/wsgi/handler.py	Tue Oct 01 16:45:58 2013 +0200
@@ -1,4 +1,4 @@
-# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
 # This file is part of CubicWeb.
@@ -97,17 +97,12 @@
     """This is the wsgi application which will be called by the
     wsgi server with the WSGI ``environ`` and ``start_response``
     parameters.
-
-    XXX: missing looping tasks and proper repository shutdown when
-    the application is stopped.
-    NOTE: no pyro
     """
 
-    def __init__(self, config, vreg=None):
-        self.appli = CubicWebPublisher(config, vreg=vreg)
+    def __init__(self, config):
+        self.appli = CubicWebPublisher(config)
         self.config = config
         self.base_url = self.config['base-url']
-        self.https_url = self.config['https-url']
         self.url_rewriter = self.appli.vreg['components'].select_or_none('urlrewriter')
 
     def _render(self, req):
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wsgi/server.py	Tue Oct 01 16:45:58 2013 +0200
@@ -0,0 +1,46 @@
+# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
+#
+# This file is part of CubicWeb.
+#
+# CubicWeb is free software: you can redistribute it and/or modify it under the
+# terms of the GNU Lesser General Public License as published by the Free
+# Software Foundation, either version 2.1 of the License, or (at your option)
+# any later version.
+#
+# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Lesser General Public License along
+# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
+"""dummy wsgi server for CubicWeb web instances"""
+
+__docformat__ = "restructuredtext en"
+
+from cubicweb.wsgi.handler import CubicWebWSGIApplication
+from cubicweb import ConfigurationError
+from wsgiref import simple_server
+
+from logging import getLogger
+LOGGER = getLogger('cubicweb')
+
+
+def run(config):
+    config.check_writeable_uid_directory(config.appdatahome)
+
+    port = config['port'] or 8080
+    interface = config['interface']
+
+    app = CubicWebWSGIApplication(config)
+    handler_cls = simple_server.WSGIRequestHandler
+    httpd = simple_server.WSGIServer((interface, port), handler_cls)
+    httpd.set_app(app)
+    repo = app.appli.repo
+    try:
+        repo.start_looping_tasks()
+        LOGGER.info('starting http server on %s', config['base-url'])
+        httpd.serve_forever()
+    finally:
+        repo.shutdown()