Mercurial > piecrust2
annotate piecrust/commands/builtin/serving.py @ 1188:a7c43131d871
bake: Fix file write flushing problem with Python 3.8+
Writing the cache files fails in Python 3.8 because it looks like flushing
behaviour has changed. We need to explicitly flush. And even then, in very
rare occurrences, it looks like it can still run into racing conditions,
so we do a very hacky and ugly "retry" loop when fetching cached data :(
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 15 Jun 2021 22:36:23 -0700 |
parents | 33a89139c284 |
children |
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 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
2 from piecrust.commands.base import ChefCommand |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
3 |
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 logger = logging.getLogger(__name__) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
6 |
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 class ServeCommand(ChefCommand): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
9 def __init__(self): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
10 super(ServeCommand, self).__init__() |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
11 self.name = 'serve' |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 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
|
13 self.cache_name = 'server' |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
14 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
15 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
|
16 parser.add_argument( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
17 '-p', '--port', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
18 help="The port for the web server", |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
19 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
|
20 parser.add_argument( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
21 '-a', '--address', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
22 help="The host for the web server", |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
23 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
|
24 parser.add_argument( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
25 '--use-reloader', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
26 help="Restart the server when PieCrust code changes", |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
27 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
|
28 parser.add_argument( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
29 '--use-debugger', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
30 help="Show the debugger when an error occurs", |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
31 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
|
32 parser.add_argument( |
890
f77f9dcba072
serve: Optionally run the admin panel with the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
33 '--admin', |
f77f9dcba072
serve: Optionally run the admin panel with the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
34 help="Also serve the administration panel.", |
f77f9dcba072
serve: Optionally run the admin panel with the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
35 action='store_true') |
f77f9dcba072
serve: Optionally run the admin panel with the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
36 parser.add_argument( |
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
37 '--wsgi', |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
38 help="The WSGI server implementation to use", |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
39 choices=['werkzeug', 'gunicorn'], |
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
40 default='werkzeug') |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
41 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
42 def run(self, ctx): |
917
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
890
diff
changeset
|
43 appfactory = ctx.appfactory |
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
44 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
|
45 port = int(ctx.args.port) |
917
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
890
diff
changeset
|
46 use_debugger = ctx.args.debug or ctx.args.use_debugger |
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
47 |
917
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
890
diff
changeset
|
48 from piecrust.serving.wrappers import run_piecrust_server |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
890
diff
changeset
|
49 run_piecrust_server( |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
890
diff
changeset
|
50 ctx.args.wsgi, appfactory, host, port, |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
890
diff
changeset
|
51 is_cmdline_mode=True, |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
890
diff
changeset
|
52 serve_admin=ctx.args.admin, |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
890
diff
changeset
|
53 use_reloader=ctx.args.use_reloader, |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
890
diff
changeset
|
54 use_debugger=use_debugger) |