Mercurial > piecrust2
comparison piecrust/serving/wrappers.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 | |
children | 9612cfc6455a |
comparison
equal
deleted
inserted
replaced
373:9fb7c4921d75 | 374:fa3ee8a8ee2d |
---|---|
1 import os | |
2 from piecrust.serving.server import Server | |
3 from piecrust.serving.procloop import _sse_abort | |
4 | |
5 | |
6 def run_werkzeug_server(root_dir, host, port, | |
7 debug_piecrust=False, sub_cache_dir=None, | |
8 use_debugger=False, use_reloader=False): | |
9 from werkzeug.serving import run_simple | |
10 | |
11 def _run_sse_check(): | |
12 # We don't want to run the processing loop here if this isn't | |
13 # the actual process that does the serving. In most cases it is, | |
14 # but if we're using Werkzeug's reloader, then it won't be the | |
15 # first time we get there... it will only be the correct process | |
16 # the second time, when the reloading process is spawned, with the | |
17 # `WERKZEUG_RUN_MAIN` variable set. | |
18 return (not use_reloader or | |
19 os.environ.get('WERKZEUG_RUN_MAIN') == 'true') | |
20 | |
21 app = _get_piecrust_server(root_dir, | |
22 debug=debug_piecrust, | |
23 sub_cache_dir=sub_cache_dir, | |
24 run_sse_check=_run_sse_check) | |
25 try: | |
26 run_simple(host, port, app, | |
27 threaded=True, | |
28 use_debugger=use_debugger, | |
29 use_reloader=use_reloader) | |
30 finally: | |
31 _sse_abort.set() | |
32 | |
33 | |
34 def run_gunicorn_server(root_dir, | |
35 debug_piecrust=False, sub_cache_dir=None, | |
36 gunicorn_options=None): | |
37 from gunicorn.app.base import BaseApplication | |
38 | |
39 class PieCrustGunicornApplication(BaseApplication): | |
40 def __init__(self, app, options): | |
41 self.app = app | |
42 self.options = options | |
43 super(PieCrustGunicornApplication, self).__init__() | |
44 | |
45 def load_config(self): | |
46 for k, v in self.options.items(): | |
47 if k in self.cfg.settings and v is not None: | |
48 self.cfg.set(k, v) | |
49 | |
50 def load(self): | |
51 return self.app | |
52 | |
53 app = _get_piecrust_server(root_dir, | |
54 debug=debug_piecrust, | |
55 sub_cache_dir=sub_cache_dir) | |
56 | |
57 gunicorn_options = gunicorn_options or {} | |
58 app_wrapper = PieCrustGunicornApplication(app, gunicorn_options) | |
59 app_wrapper.run() | |
60 | |
61 | |
62 def _get_piecrust_server(root_dir, **kwargs): | |
63 server = Server(root_dir, **kwargs) | |
64 app = server.getWsgiApp() | |
65 return app | |
66 |