comparison piecrust/serving/wrappers.py @ 935:7ecb946bfafd

admin: Lots of fixes for running the admin panel in a WSGI server. - Use new source APIs in the dashboard to open WIP files. - Fixed broken/outdated code in some views. - Fixed cases when Flask is not running at the root URL by using the `SCRIPT_NAME` variable somewhat more properly.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 04 Oct 2017 09:15:16 -0700
parents 33a89139c284
children 137c9b41edd2
comparison
equal deleted inserted replaced
934:98430e7143d2 935:7ecb946bfafd
140 gunicorn_options = gunicorn_options or {} 140 gunicorn_options = gunicorn_options or {}
141 app_wrapper = PieCrustGunicornApplication(app, gunicorn_options) 141 app_wrapper = PieCrustGunicornApplication(app, gunicorn_options)
142 app_wrapper.run() 142 app_wrapper.run()
143 143
144 144
145 def get_piecrust_server(root_dir, *,
146 debug=False,
147 cache_key=None,
148 serve_site=True,
149 serve_admin=False,
150 is_cmdline_mode=False):
151 from piecrust.app import PieCrustFactory
152 appfactory = PieCrustFactory(root_dir,
153 debug=debug,
154 cache_key=cache_key)
155 return _get_piecrust_server(appfactory,
156 serve_site=serve_site,
157 serve_admin=serve_admin,
158 is_cmdline_mode=is_cmdline_mode)
159
160
145 def _get_piecrust_server(appfactory, *, 161 def _get_piecrust_server(appfactory, *,
146 serve_site=True, 162 serve_site=True,
147 serve_admin=False, 163 serve_admin=False,
148 is_cmdline_mode=False, 164 is_cmdline_mode=False,
165 admin_root_url=None,
149 run_sse_check=None): 166 run_sse_check=None):
150 app = None 167 app = None
151 168
152 if serve_site: 169 if serve_site:
153 from piecrust.serving.middlewares import ( 170 from piecrust.serving.middlewares import (
162 app, appfactory, run_sse_check=run_sse_check) 179 app, appfactory, run_sse_check=run_sse_check)
163 180
164 if serve_admin: 181 if serve_admin:
165 from piecrust.admin.web import create_foodtruck_app 182 from piecrust.admin.web import create_foodtruck_app
166 183
167 admin_root_url = '/pc-admin'
168 es = { 184 es = {
169 'FOODTRUCK_CMDLINE_MODE': is_cmdline_mode, 185 'FOODTRUCK_CMDLINE_MODE': is_cmdline_mode,
170 'FOODTRUCK_ROOT': appfactory.root_dir, 186 'FOODTRUCK_ROOT_DIR': appfactory.root_dir,
171 'FOODTRUCK_URL_PREFIX': admin_root_url, 187 'FOODTRUCK_ROOT_URL': admin_root_url,
172 'DEBUG': appfactory.debug} 188 'DEBUG': appfactory.debug}
173 if is_cmdline_mode: 189 if is_cmdline_mode:
174 es.update({ 190 es.update({
175 'SECRET_KEY': os.urandom(22), 191 'SECRET_KEY': os.urandom(22),
176 'LOGIN_DISABLED': True}) 192 'LOGIN_DISABLED': True})
177 193
178 if appfactory.debug and is_cmdline_mode: 194 if appfactory.debug and is_cmdline_mode:
179 # Disable PIN protection with Werkzeug's debugger. 195 # Disable PIN protection with Werkzeug's debugger.
180 os.environ['WERKZEUG_DEBUG_PIN'] = 'off' 196 os.environ['WERKZEUG_DEBUG_PIN'] = 'off'
181 197
182 admin_app = create_foodtruck_app(es) 198 admin_app = create_foodtruck_app(es, url_prefix=admin_root_url)
183 admin_app.wsgi_app = _PieCrustSiteOrAdminMiddleware( 199 if app is not None:
184 app, admin_app.wsgi_app, admin_root_url) 200 admin_app.wsgi_app = _PieCrustSiteOrAdminMiddleware(
201 app, admin_app.wsgi_app, admin_root_url)
202
185 app = admin_app 203 app = admin_app
186 204
187 return app 205 return app
188 206
189 207
201 def __call__(self, environ, start_response): 219 def __call__(self, environ, start_response):
202 path_info = environ.get('PATH_INFO', '') 220 path_info = environ.get('PATH_INFO', '')
203 if path_info.startswith(self.admin_root_url): 221 if path_info.startswith(self.admin_root_url):
204 return self.admin_app(environ, start_response) 222 return self.admin_app(environ, start_response)
205 return self.main_app(environ, start_response) 223 return self.main_app(environ, start_response)
224
225
226 class _PieCrustAdminScriptNamePatcherMiddleware:
227 def __init__(self, admin_app, admin_root_url):
228 self.admin_app = admin_app
229 self.admin_root_url = '/%s' % admin_root_url.strip('/')
230
231 def __call__(self, environ, start_response):
232 environ['SCRIPT_NAME'] = self.admin_root_url
233 return self.admin_app(environ, start_response)