Mercurial > piecrust2
annotate piecrust/serving/wrappers.py @ 1165:a928ee22c20a
cm: Upgrade Jinja2 version.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Fri, 04 Oct 2019 09:00:57 -0700 |
parents | 587bccf72d75 |
children |
rev | line source |
---|---|
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
1 import os |
558
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
2 import signal |
552
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
3 import logging |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
4 |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
5 |
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
6 logger = logging.getLogger(__name__) |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 |
917
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
9 def run_piecrust_server(wsgi, appfactory, host, port, |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
10 is_cmdline_mode=False, |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
11 serve_admin=False, |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
12 use_debugger=False, |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
13 use_reloader=False): |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
14 |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
15 if wsgi == 'werkzeug': |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
16 _run_werkzeug_server(appfactory, host, port, |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
17 is_cmdline_mode=is_cmdline_mode, |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
18 serve_admin=serve_admin, |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
19 use_debugger=use_debugger, |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
20 use_reloader=use_reloader) |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
21 |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
22 elif wsgi == 'gunicorn': |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
23 options = { |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
24 'bind': '%s:%s' % (host, port), |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
25 'accesslog': '-', # print access log to stderr |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
26 } |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
27 if use_debugger: |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
28 options['loglevel'] = 'debug' |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
29 if use_reloader: |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
30 options['reload'] = True |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
31 _run_gunicorn_server(appfactory, |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
32 is_cmdline_mode=is_cmdline_mode, |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
33 gunicorn_options=options) |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
34 |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
35 else: |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
36 raise Exception("Unknown WSGI server: %s" % wsgi) |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
37 |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
38 |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
39 def _run_werkzeug_server(appfactory, host, port, *, |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
40 is_cmdline_mode=False, |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
41 serve_admin=False, |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
42 use_debugger=False, |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
43 use_reloader=False): |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
44 from werkzeug.serving import run_simple |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
45 |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
46 def _run_sse_check(): |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
47 # We don't want to run the processing loop here if this isn't |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
48 # the actual process that does the serving. In most cases it is, |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
49 # but if we're using Werkzeug's reloader, then it won't be the |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
50 # first time we get there... it will only be the correct process |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
51 # the second time, when the reloading process is spawned, with the |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
52 # `WERKZEUG_RUN_MAIN` variable set. |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
53 return (not use_reloader or |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
54 os.environ.get('WERKZEUG_RUN_MAIN') == 'true') |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
55 |
666
81d9c3a3a0b5
internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents:
663
diff
changeset
|
56 app = _get_piecrust_server(appfactory, |
917
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
57 is_cmdline_mode=is_cmdline_mode, |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
58 serve_site=True, |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
59 serve_admin=serve_admin, |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
60 run_sse_check=_run_sse_check) |
552
9612cfc6455a
serve: Rewrite of the Server-Sent Event code for build notifications.
Ludovic Chabant <ludovic@chabant.com>
parents:
374
diff
changeset
|
61 |
558
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
62 # We need to do a few things to get Werkzeug to properly shutdown or |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
63 # restart while SSE responses are running. This is because Werkzeug runs |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
64 # them in background threads (because we tell it to), but those threads |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
65 # are not marked as "daemon", so when the main thread tries to exit, it |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
66 # will wait on those SSE responses to end, which will pretty much never |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
67 # happen (except for a timeout or the user closing their browser). |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
68 # |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
69 # In theory we should be using a proper async server for this kind of |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
70 # stuff, but I'd rather avoid additional dependencies on stuff that's not |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
71 # necessarily super portable. |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
72 # |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
73 # Anyway, we run the server as usual, but we intercept the `SIGINT` |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
74 # signal for when the user presses `CTRL-C`. When that happens, we set a |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
75 # flag that will make all existing SSE loops return, which will make it |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
76 # possible for the main thread to end too. |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
77 # |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
78 # We also need to do a few thing for the "reloading" feature in Werkzeug, |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
79 # see the comment down there for more info. |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
80 def _shutdown_server(): |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
81 from piecrust.serving import procloop |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
82 procloop.server_shutdown = True |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
83 |
917
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
84 if serve_admin: |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
85 from piecrust.admin import pubutil |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
86 pubutil.server_shutdown = True |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
87 |
558
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
88 def _shutdown_server_and_raise_sigint(): |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
89 if not use_reloader or os.environ.get('WERKZEUG_RUN_MAIN') == 'true': |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
90 # We only need to shutdown the SSE requests for the process |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
91 # that actually runs them. |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
92 print("") |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
93 print("Shutting server down...") |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
94 _shutdown_server() |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
95 raise KeyboardInterrupt() |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
96 |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
97 signal.signal(signal.SIGINT, |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
98 lambda *args: _shutdown_server_and_raise_sigint()) |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
99 |
865
1bb0d973dc69
serve: Disable Werkzeug's debugger PIN.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
100 # Disable debugger PIN protection. |
1bb0d973dc69
serve: Disable Werkzeug's debugger PIN.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
101 os.environ['WERKZEUG_DEBUG_PIN'] = 'off' |
1bb0d973dc69
serve: Disable Werkzeug's debugger PIN.
Ludovic Chabant <ludovic@chabant.com>
parents:
852
diff
changeset
|
102 |
1122
587bccf72d75
serve: Only tell about the admin panel if needed.
Ludovic Chabant <ludovic@chabant.com>
parents:
1117
diff
changeset
|
103 if is_cmdline_mode and serve_admin: |
1117
e8511fed42a3
serve: Indicate where to find the admin panel in the console output.
Ludovic Chabant <ludovic@chabant.com>
parents:
1109
diff
changeset
|
104 admin_url = 'http://%s:%s%s' % (host, port, '/pc-admin') |
e8511fed42a3
serve: Indicate where to find the admin panel in the console output.
Ludovic Chabant <ludovic@chabant.com>
parents:
1109
diff
changeset
|
105 logger.info("The administrative panel is available at: %s" % |
e8511fed42a3
serve: Indicate where to find the admin panel in the console output.
Ludovic Chabant <ludovic@chabant.com>
parents:
1109
diff
changeset
|
106 admin_url) |
e8511fed42a3
serve: Indicate where to find the admin panel in the console output.
Ludovic Chabant <ludovic@chabant.com>
parents:
1109
diff
changeset
|
107 |
558
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
108 try: |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
109 run_simple(host, port, app, |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
110 threaded=True, |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
111 use_debugger=use_debugger, |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
112 use_reloader=use_reloader) |
558
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
113 except SystemExit: |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
114 if os.environ.get('WERKZEUG_RUN_MAIN') == 'true': |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
115 # When using the reloader, if code has changed, the child process |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
116 # will use `sys.exit` to end and let the master process restart |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
117 # it... we need to shutdown the SSE requests otherwise it will |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
118 # not exit. |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
119 _shutdown_server() |
9ab005db2592
serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents:
553
diff
changeset
|
120 raise |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
121 |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
122 |
917
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
123 def _run_gunicorn_server(appfactory, |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
124 is_cmdline_mode=False, |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
125 gunicorn_options=None): |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
126 from gunicorn.app.base import BaseApplication |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
127 |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
128 class PieCrustGunicornApplication(BaseApplication): |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
129 def __init__(self, app, options): |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
130 self.app = app |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
131 self.options = options |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
132 super(PieCrustGunicornApplication, self).__init__() |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
133 |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
134 def load_config(self): |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
135 for k, v in self.options.items(): |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
136 if k in self.cfg.settings and v is not None: |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
137 self.cfg.set(k, v) |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
138 |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
139 def load(self): |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
140 return self.app |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
141 |
917
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
142 app = _get_piecrust_server(appfactory, |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
143 is_cmdline_mode=is_cmdline_mode) |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
144 |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
145 gunicorn_options = gunicorn_options or {} |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
146 app_wrapper = PieCrustGunicornApplication(app, gunicorn_options) |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
147 app_wrapper.run() |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
148 |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
149 |
935
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
150 def get_piecrust_server(root_dir, *, |
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
151 debug=False, |
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
152 cache_key=None, |
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
153 serve_site=True, |
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
154 serve_admin=False, |
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
155 is_cmdline_mode=False): |
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
156 from piecrust.app import PieCrustFactory |
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
157 appfactory = PieCrustFactory(root_dir, |
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
158 debug=debug, |
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
159 cache_key=cache_key) |
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
160 return _get_piecrust_server(appfactory, |
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
161 serve_site=serve_site, |
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
162 serve_admin=serve_admin, |
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
163 is_cmdline_mode=is_cmdline_mode) |
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
164 |
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
165 |
917
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
166 def _get_piecrust_server(appfactory, *, |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
167 serve_site=True, |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
168 serve_admin=False, |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
169 is_cmdline_mode=False, |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
170 run_sse_check=None): |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
171 app = None |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
172 |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
173 if serve_site: |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
174 from piecrust.serving.middlewares import ( |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
175 PieCrustStaticResourcesMiddleware, PieCrustDebugMiddleware) |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
176 from piecrust.serving.server import PieCrustServer |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
177 |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
178 app = PieCrustServer(appfactory) |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
179 app = PieCrustStaticResourcesMiddleware(app) |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
180 |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
181 if is_cmdline_mode: |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
182 app = PieCrustDebugMiddleware( |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
183 app, appfactory, run_sse_check=run_sse_check) |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
184 |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
185 if serve_admin: |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
186 from piecrust.admin.web import create_foodtruck_app |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
187 |
1109
6f26e83dfced
admin: Fix another root URL issue for the admin panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
1068
diff
changeset
|
188 admin_root_url = ('/pc-admin' if is_cmdline_mode else None) |
1068
137c9b41edd2
serve: Fix crash with `serve --admin`.
Ludovic Chabant <ludovic@chabant.com>
parents:
935
diff
changeset
|
189 |
917
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
190 es = { |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
191 'FOODTRUCK_CMDLINE_MODE': is_cmdline_mode, |
935
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
192 'FOODTRUCK_ROOT_DIR': appfactory.root_dir, |
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
193 'FOODTRUCK_ROOT_URL': admin_root_url, |
917
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
194 'DEBUG': appfactory.debug} |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
195 if is_cmdline_mode: |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
196 es.update({ |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
197 'SECRET_KEY': os.urandom(22), |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
198 'LOGIN_DISABLED': True}) |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
199 |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
200 if appfactory.debug and is_cmdline_mode: |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
201 # Disable PIN protection with Werkzeug's debugger. |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
202 os.environ['WERKZEUG_DEBUG_PIN'] = 'off' |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
203 |
935
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
204 admin_app = create_foodtruck_app(es, url_prefix=admin_root_url) |
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
205 if app is not None: |
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
206 admin_app.wsgi_app = _PieCrustSiteOrAdminMiddleware( |
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
207 app, admin_app.wsgi_app, admin_root_url) |
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
208 |
917
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
209 app = admin_app |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
210 |
374
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
211 return app |
fa3ee8a8ee2d
serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
212 |
917
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
213 |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
214 class _PieCrustSiteOrAdminMiddleware: |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
215 def __init__(self, main_app, admin_app, admin_root_url): |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
216 from werkzeug.exceptions import abort |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
217 |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
218 def _err_resp(e, sr): |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
219 abort(404) |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
220 |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
221 self.main_app = main_app |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
222 self.admin_app = admin_app or _err_resp |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
223 self.admin_root_url = admin_root_url |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
224 |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
225 def __call__(self, environ, start_response): |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
226 path_info = environ.get('PATH_INFO', '') |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
227 if path_info.startswith(self.admin_root_url): |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
228 return self.admin_app(environ, start_response) |
33a89139c284
serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents:
865
diff
changeset
|
229 return self.main_app(environ, start_response) |
935
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
230 |
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
231 |
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
232 class _PieCrustAdminScriptNamePatcherMiddleware: |
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
233 def __init__(self, admin_app, admin_root_url): |
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
234 self.admin_app = admin_app |
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
235 self.admin_root_url = '/%s' % admin_root_url.strip('/') |
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
236 |
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
237 def __call__(self, environ, start_response): |
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
238 environ['SCRIPT_NAME'] = self.admin_root_url |
7ecb946bfafd
admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents:
917
diff
changeset
|
239 return self.admin_app(environ, start_response) |