Mercurial > piecrust2
annotate piecrust/commands/builtin/serving.py @ 535:03c3a77fda60 2.0.0b2
prepare: More help about scaffolding.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Wed, 29 Jul 2015 09:13:54 -0700 |
parents | fa3ee8a8ee2d |
children | 3ceeca7bb71c |
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 |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
371
diff
changeset
|
3 from piecrust.serving.wrappers import run_werkzeug_server, run_gunicorn_server |
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 |
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): |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
371
diff
changeset
|
40 root_dir = 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
|
41 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
|
42 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
|
43 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
|
44 |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
45 if ctx.args.wsgi == 'werkzeug': |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
371
diff
changeset
|
46 run_werkzeug_server( |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
371
diff
changeset
|
47 root_dir, host, port, |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
371
diff
changeset
|
48 debug_piecrust=debug, |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
371
diff
changeset
|
49 sub_cache_dir=ctx.app.sub_cache_dir, |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
371
diff
changeset
|
50 use_debugger=debug, |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
371
diff
changeset
|
51 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
|
52 |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
53 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
|
54 options = { |
d7a548ebcd58
serve: Add server sent events for showing pipeline errors in the debug window.
Ludovic Chabant <ludovic@chabant.com>
parents:
169
diff
changeset
|
55 'bind': '%s:%s' % (host, port), |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
371
diff
changeset
|
56 'accesslog': '-', # print access log to stderr |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
371
diff
changeset
|
57 } |
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 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
|
59 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
|
60 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
|
61 options['reload'] = True |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
371
diff
changeset
|
62 run_gunicorn_server( |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
371
diff
changeset
|
63 root_dir, |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
371
diff
changeset
|
64 debug_piecrust=debug, |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
371
diff
changeset
|
65 sub_cache_dir=ctx.app.sub_cache_dir, |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
371
diff
changeset
|
66 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 |