annotate piecrust/serving/wrappers.py @ 1051:971b4d67e82a

serve: Fix problems with assets disappearing between servings. When an asset file changes, its source's pipeline is re-run. But that created a bake record that only had that pipeline's output, so the other outputs were incorrectly considered empty and therefore any stray files were removed. Now we copy over bake records for the pipelines we don't run.
author Ludovic Chabant <ludovic@chabant.com>
date Fri, 26 Jan 2018 18:05:02 -0800
parents 7ecb946bfafd
children 137c9b41edd2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
558
9ab005db2592 serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents: 553
diff changeset
103 try:
374
fa3ee8a8ee2d serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
104 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
105 threaded=True,
fa3ee8a8ee2d serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
106 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
107 use_reloader=use_reloader)
558
9ab005db2592 serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents: 553
diff changeset
108 except SystemExit:
9ab005db2592 serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents: 553
diff changeset
109 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
110 # 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
111 # 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
112 # 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
113 # not exit.
9ab005db2592 serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents: 553
diff changeset
114 _shutdown_server()
9ab005db2592 serve: Improve reloading and shutdown of the preview server.
Ludovic Chabant <ludovic@chabant.com>
parents: 553
diff changeset
115 raise
374
fa3ee8a8ee2d serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
116
fa3ee8a8ee2d serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
117
917
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
118 def _run_gunicorn_server(appfactory,
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
119 is_cmdline_mode=False,
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
120 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
121 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
122
fa3ee8a8ee2d serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
123 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
124 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
125 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
126 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
127 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
128
fa3ee8a8ee2d serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
129 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
130 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
131 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
132 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
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(self):
fa3ee8a8ee2d serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
135 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
136
917
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
137 app = _get_piecrust_server(appfactory,
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
138 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
139
fa3ee8a8ee2d serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
140 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
141 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
142 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
143
fa3ee8a8ee2d serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
144
935
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 917
diff changeset
145 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
146 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
147 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
148 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
149 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
150 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
151 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
152 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
153 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
154 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
155 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
156 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
157 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
158 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
159
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 917
diff changeset
160
917
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
161 def _get_piecrust_server(appfactory, *,
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
162 serve_site=True,
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
163 serve_admin=False,
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
164 is_cmdline_mode=False,
935
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 917
diff changeset
165 admin_root_url=None,
917
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
166 run_sse_check=None):
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
167 app = None
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
168
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
169 if serve_site:
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
170 from piecrust.serving.middlewares import (
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
171 PieCrustStaticResourcesMiddleware, PieCrustDebugMiddleware)
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
172 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
173
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
174 app = PieCrustServer(appfactory)
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
175 app = PieCrustStaticResourcesMiddleware(app)
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
176
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
177 if is_cmdline_mode:
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
178 app = PieCrustDebugMiddleware(
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
179 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
180
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
181 if serve_admin:
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
182 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
183
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
184 es = {
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
185 '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
186 '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
187 '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
188 'DEBUG': appfactory.debug}
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
189 if is_cmdline_mode:
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
190 es.update({
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
191 'SECRET_KEY': os.urandom(22),
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
192 'LOGIN_DISABLED': True})
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
193
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
194 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
195 # 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
196 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
197
935
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 917
diff changeset
198 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
199 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
200 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
201 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
202
917
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
203 app = admin_app
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
204
374
fa3ee8a8ee2d serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
205 return app
fa3ee8a8ee2d serve: Split the server code in a couple modules inside a `serving` package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
206
917
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
207
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
208 class _PieCrustSiteOrAdminMiddleware:
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
209 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
210 from werkzeug.exceptions import abort
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
211
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
212 def _err_resp(e, sr):
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
213 abort(404)
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
214
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
215 self.main_app = main_app
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
216 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
217 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
218
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 865
diff changeset
219 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
220 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
221 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
222 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
223 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
224
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 917
diff changeset
225
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 917
diff changeset
226 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
227 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
228 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
229 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
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 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
232 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
233 return self.admin_app(environ, start_response)