diff piecrust/commands/builtin/serving.py @ 374:fa3ee8a8ee2d

serve: Split the server code in a couple modules inside a `serving` package. This makes the `serve` command's code a bit more removed from implementation details, and paves the way for the CMS mode.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 07 May 2015 21:37:38 -0700
parents c2ca72fb7f0b
children 3ceeca7bb71c
line wrap: on
line diff
--- a/piecrust/commands/builtin/serving.py	Thu May 07 21:36:17 2015 -0700
+++ b/piecrust/commands/builtin/serving.py	Thu May 07 21:37:38 2015 -0700
@@ -1,6 +1,6 @@
 import logging
-from piecrust.serving import Server, _sse_abort
 from piecrust.commands.base import ChefCommand
+from piecrust.serving.wrappers import run_werkzeug_server, run_gunicorn_server
 
 
 logger = logging.getLogger(__name__)
@@ -37,54 +37,31 @@
                 default='werkzeug')
 
     def run(self, ctx):
+        root_dir = ctx.app.root_dir
         host = ctx.args.address
         port = int(ctx.args.port)
         debug = ctx.args.debug or ctx.args.use_debugger
 
-        server = Server(
-                ctx.app.root_dir,
-                debug=debug,
-                sub_cache_dir=ctx.app.sub_cache_dir,
-                use_reloader=ctx.args.use_reloader)
-        app = server.getWsgiApp()
-
         if ctx.args.wsgi == 'werkzeug':
-            from werkzeug.serving import run_simple
-            try:
-                run_simple(host, port, app,
-                           threaded=True,
-                           use_debugger=debug,
-                           use_reloader=ctx.args.use_reloader)
-            finally:
-                _sse_abort.set()
+            run_werkzeug_server(
+                    root_dir, host, port,
+                    debug_piecrust=debug,
+                    sub_cache_dir=ctx.app.sub_cache_dir,
+                    use_debugger=debug,
+                    use_reloader=ctx.args.use_reloader)
 
         elif ctx.args.wsgi == 'gunicorn':
-            from gunicorn.app.base import BaseApplication
-
-            class PieCrustGunicornApplication(BaseApplication):
-                def __init__(self, app, options):
-                    self.app = app
-                    self.options = options
-                    super(PieCrustGunicornApplication, self).__init__()
-
-                def load_config(self):
-                    for k, v in self.options.items():
-                        if k in self.cfg.settings and v is not None:
-                            self.cfg.set(k, v)
-
-                def load(self):
-                    return self.app
-
             options = {
                     'bind': '%s:%s' % (host, port),
-                    'accesslog': '-',
-                    'worker_class': 'gaiohttp',
-                    'workers': 2,
-                    'timeout': 999999}
+                    'accesslog': '-',  # print access log to stderr
+                    }
             if debug:
                 options['loglevel'] = 'debug'
             if ctx.args.use_reloader:
                 options['reload'] = True
-            app_wrapper = PieCrustGunicornApplication(app, options)
-            app_wrapper.run()
+            run_gunicorn_server(
+                    root_dir,
+                    debug_piecrust=debug,
+                    sub_cache_dir=ctx.app.sub_cache_dir,
+                    gunicorn_options=options)