Mercurial > piecrust2
annotate piecrust/commands/builtin/serving.py @ 904:cc2647360036
internal: Remove unnecessary timer, add timer for lazy data building.
| author | Ludovic Chabant <ludovic@chabant.com> |
|---|---|
| date | Sun, 23 Jul 2017 08:25:45 -0700 |
| parents | f77f9dcba072 |
| children | 33a89139c284 |
| 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): |
|
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
43 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
|
44 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
|
45 debug = ctx.args.debug or ctx.args.use_debugger |
|
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
46 appfactory = ctx.appfactory |
|
666
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
47 |
|
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
48 if ctx.args.wsgi == 'werkzeug': |
|
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
49 from piecrust.serving.wrappers import run_werkzeug_server |
|
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
371
diff
changeset
|
50 run_werkzeug_server( |
|
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
51 appfactory, host, port, |
|
890
f77f9dcba072
serve: Optionally run the admin panel with the server.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
52 serve_admin=ctx.args.admin, |
|
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
53 use_debugger=debug, |
|
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
54 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
|
55 |
|
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
56 elif ctx.args.wsgi == 'gunicorn': |
|
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
57 from piecrust.serving.wrappers import run_gunicorn_server |
|
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
58 options = { |
|
852
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
59 'bind': '%s:%s' % (host, port), |
|
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
60 'accesslog': '-', # print access log to stderr |
|
4850f8c21b6e
core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents:
666
diff
changeset
|
61 } |
|
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
62 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
|
63 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
|
64 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
|
65 options['reload'] = True |
|
666
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
66 run_gunicorn_server(appfactory, gunicorn_options=options) |
|
219
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
67 |
