Mercurial > piecrust2
annotate piecrust/commands/builtin/serving.py @ 371:c2ca72fb7f0b 2.0.0a8
caching: Use separate caches for config variants and other contexts.
* The `_cache` directory is now organized in multiple "sub-caches" for
different contexts.
* A new context is created when config variants or overrides are applied.
* `serve` context uses a different context that the other commends, to prevent
the `bake` command's output from messing up the preview server (e.g. with
how asset URLs are generated differently between the two).
* Fix a few places where the cache directory was referenced directly.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 03 May 2015 23:59:46 -0700 |
parents | 4f00bb99400e |
children | fa3ee8a8ee2d |
rev | line source |
---|---|
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
1 import logging |
224
4f00bb99400e
serve: Fix exiting the server with `CTRL+C` when the SSE response is running.
Ludovic Chabant <ludovic@chabant.com>
parents:
219
diff
changeset
|
2 from piecrust.serving import Server, _sse_abort |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
3 from piecrust.commands.base import ChefCommand |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
4 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
5 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
6 logger = logging.getLogger(__name__) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
9 class ServeCommand(ChefCommand): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
10 def __init__(self): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
11 super(ServeCommand, self).__init__() |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 self.name = 'serve' |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 self.description = "Runs a local web server to serve your website." |
371
c2ca72fb7f0b
caching: Use separate caches for config variants and other contexts.
Ludovic Chabant <ludovic@chabant.com>
parents:
224
diff
changeset
|
14 self.cache_name = 'server' |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
15 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
16 def setupParser(self, parser, app): |
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
17 parser.add_argument( |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
18 '-p', '--port', |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
19 help="The port for the web server", |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
20 default=8080) |
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
21 parser.add_argument( |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
22 '-a', '--address', |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
23 help="The host for the web server", |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
24 default='localhost') |
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
25 parser.add_argument( |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
26 '--use-reloader', |
135
50b65c700c96
Don't use Werkzeug's reloader in non-debug mode unless we ask for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
27 help="Restart the server when PieCrust code changes", |
50b65c700c96
Don't use Werkzeug's reloader in non-debug mode unless we ask for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
28 action='store_true') |
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
29 parser.add_argument( |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
30 '--use-debugger', |
169
f3aa511eef99
serve: Add option to use the debugger without `--debug`.
Ludovic Chabant <ludovic@chabant.com>
parents:
135
diff
changeset
|
31 help="Show the debugger when an error occurs", |
f3aa511eef99
serve: Add option to use the debugger without `--debug`.
Ludovic Chabant <ludovic@chabant.com>
parents:
135
diff
changeset
|
32 action='store_true') |
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
33 parser.add_argument( |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
34 '--wsgi', |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
35 help="The WSGI server implementation to use", |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
36 choices=['werkzeug', 'gunicorn'], |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
37 default='werkzeug') |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
38 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
39 def run(self, ctx): |
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
40 host = ctx.args.address |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
41 port = int(ctx.args.port) |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
42 debug = ctx.args.debug or ctx.args.use_debugger |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
43 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
44 server = Server( |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
45 ctx.app.root_dir, |
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
46 debug=debug, |
371
c2ca72fb7f0b
caching: Use separate caches for config variants and other contexts.
Ludovic Chabant <ludovic@chabant.com>
parents:
224
diff
changeset
|
47 sub_cache_dir=ctx.app.sub_cache_dir, |
135
50b65c700c96
Don't use Werkzeug's reloader in non-debug mode unless we ask for it.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
48 use_reloader=ctx.args.use_reloader) |
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
49 app = server.getWsgiApp() |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
50 |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
51 if ctx.args.wsgi == 'werkzeug': |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
52 from werkzeug.serving import run_simple |
224
4f00bb99400e
serve: Fix exiting the server with `CTRL+C` when the SSE response is running.
Ludovic Chabant <ludovic@chabant.com>
parents:
219
diff
changeset
|
53 try: |
4f00bb99400e
serve: Fix exiting the server with `CTRL+C` when the SSE response is running.
Ludovic Chabant <ludovic@chabant.com>
parents:
219
diff
changeset
|
54 run_simple(host, port, app, |
4f00bb99400e
serve: Fix exiting the server with `CTRL+C` when the SSE response is running.
Ludovic Chabant <ludovic@chabant.com>
parents:
219
diff
changeset
|
55 threaded=True, |
4f00bb99400e
serve: Fix exiting the server with `CTRL+C` when the SSE response is running.
Ludovic Chabant <ludovic@chabant.com>
parents:
219
diff
changeset
|
56 use_debugger=debug, |
4f00bb99400e
serve: Fix exiting the server with `CTRL+C` when the SSE response is running.
Ludovic Chabant <ludovic@chabant.com>
parents:
219
diff
changeset
|
57 use_reloader=ctx.args.use_reloader) |
4f00bb99400e
serve: Fix exiting the server with `CTRL+C` when the SSE response is running.
Ludovic Chabant <ludovic@chabant.com>
parents:
219
diff
changeset
|
58 finally: |
4f00bb99400e
serve: Fix exiting the server with `CTRL+C` when the SSE response is running.
Ludovic Chabant <ludovic@chabant.com>
parents:
219
diff
changeset
|
59 _sse_abort.set() |
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
60 |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
61 elif ctx.args.wsgi == 'gunicorn': |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
62 from gunicorn.app.base import BaseApplication |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
63 |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
64 class PieCrustGunicornApplication(BaseApplication): |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
65 def __init__(self, app, options): |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
66 self.app = app |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
67 self.options = options |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
68 super(PieCrustGunicornApplication, self).__init__() |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
69 |
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
70 def load_config(self): |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
71 for k, v in self.options.items(): |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
72 if k in self.cfg.settings and v is not None: |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
73 self.cfg.set(k, v) |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
74 |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
75 def load(self): |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
76 return self.app |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
77 |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
78 options = { |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
79 'bind': '%s:%s' % (host, port), |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
80 'accesslog': '-', |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
81 'worker_class': 'gaiohttp', |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
82 'workers': 2, |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
83 'timeout': 999999} |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
84 if debug: |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
85 options['loglevel'] = 'debug' |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
86 if ctx.args.use_reloader: |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
87 options['reload'] = True |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
88 app_wrapper = PieCrustGunicornApplication(app, options) |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
89 app_wrapper.run() |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
90 |