comparison 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
comparison
equal deleted inserted replaced
373:9fb7c4921d75 374:fa3ee8a8ee2d
1 import logging 1 import logging
2 from piecrust.serving import Server, _sse_abort
3 from piecrust.commands.base import ChefCommand 2 from piecrust.commands.base import ChefCommand
3 from piecrust.serving.wrappers import run_werkzeug_server, run_gunicorn_server
4 4
5 5
6 logger = logging.getLogger(__name__) 6 logger = logging.getLogger(__name__)
7 7
8 8
35 help="The WSGI server implementation to use", 35 help="The WSGI server implementation to use",
36 choices=['werkzeug', 'gunicorn'], 36 choices=['werkzeug', 'gunicorn'],
37 default='werkzeug') 37 default='werkzeug')
38 38
39 def run(self, ctx): 39 def run(self, ctx):
40 root_dir = ctx.app.root_dir
40 host = ctx.args.address 41 host = ctx.args.address
41 port = int(ctx.args.port) 42 port = int(ctx.args.port)
42 debug = ctx.args.debug or ctx.args.use_debugger 43 debug = ctx.args.debug or ctx.args.use_debugger
43 44
44 server = Server(
45 ctx.app.root_dir,
46 debug=debug,
47 sub_cache_dir=ctx.app.sub_cache_dir,
48 use_reloader=ctx.args.use_reloader)
49 app = server.getWsgiApp()
50
51 if ctx.args.wsgi == 'werkzeug': 45 if ctx.args.wsgi == 'werkzeug':
52 from werkzeug.serving import run_simple 46 run_werkzeug_server(
53 try: 47 root_dir, host, port,
54 run_simple(host, port, app, 48 debug_piecrust=debug,
55 threaded=True, 49 sub_cache_dir=ctx.app.sub_cache_dir,
56 use_debugger=debug, 50 use_debugger=debug,
57 use_reloader=ctx.args.use_reloader) 51 use_reloader=ctx.args.use_reloader)
58 finally:
59 _sse_abort.set()
60 52
61 elif ctx.args.wsgi == 'gunicorn': 53 elif ctx.args.wsgi == 'gunicorn':
62 from gunicorn.app.base import BaseApplication
63
64 class PieCrustGunicornApplication(BaseApplication):
65 def __init__(self, app, options):
66 self.app = app
67 self.options = options
68 super(PieCrustGunicornApplication, self).__init__()
69
70 def load_config(self):
71 for k, v in self.options.items():
72 if k in self.cfg.settings and v is not None:
73 self.cfg.set(k, v)
74
75 def load(self):
76 return self.app
77
78 options = { 54 options = {
79 'bind': '%s:%s' % (host, port), 55 'bind': '%s:%s' % (host, port),
80 'accesslog': '-', 56 'accesslog': '-', # print access log to stderr
81 'worker_class': 'gaiohttp', 57 }
82 'workers': 2,
83 'timeout': 999999}
84 if debug: 58 if debug:
85 options['loglevel'] = 'debug' 59 options['loglevel'] = 'debug'
86 if ctx.args.use_reloader: 60 if ctx.args.use_reloader:
87 options['reload'] = True 61 options['reload'] = True
88 app_wrapper = PieCrustGunicornApplication(app, options) 62 run_gunicorn_server(
89 app_wrapper.run() 63 root_dir,
64 debug_piecrust=debug,
65 sub_cache_dir=ctx.app.sub_cache_dir,
66 gunicorn_options=options)
90 67