##// END OF EJS Templates
vcs: Move VCSMiddleware up to pyramid layer as wrapper around pylons app....
Martin Bornhold -
r581:5c1ed3a7 default
parent child Browse files
Show More
@@ -126,10 +126,6 b' def make_app(global_conf, full_stack=Tru'
126 126 # Appenlight monitoring and error handler
127 127 app, appenlight_client = wrap_in_appenlight_if_enabled(app, config)
128 128
129 # we want our low level middleware to get to the request ASAP. We don't
130 # need any pylons stack middleware in them
131 app = VCSMiddleware(app, config, appenlight_client)
132
133 129 # Establish the Registry for this application
134 130 app = RegistryManager(app)
135 131
@@ -178,6 +174,57 b' def make_pyramid_app(global_config, **se'
178 174 return pyramid_app
179 175
180 176
177 def make_not_found_view(config):
178 """
179 This creates the view shich should be registered as not-found-view to
180 pyramid. Basically it contains of the old pylons app, converted to a view.
181 Additionally it is wrapped by some other middlewares.
182 """
183 settings = config.registry.settings
184
185 # Make pylons app from unprepared settings.
186 pylons_app = make_app(
187 config.registry._pylons_compat_global_config,
188 **config.registry._pylons_compat_settings)
189 config.registry._pylons_compat_config = pylons_app.config
190
191 # The VCSMiddleware shall operate like a fallback if pyramid doesn't find
192 # a view to handle the request. Therefore we wrap it around the pylons app
193 # and it will be added as not found view.
194 if settings['vcs.server.enable']:
195 pylons_app = VCSMiddleware(
196 pylons_app, settings, None, registry=config.registry)
197
198 pylons_app_as_view = wsgiapp(pylons_app)
199
200 # Protect from VCS Server error related pages when server is not available
201 vcs_server_enabled = asbool(settings.get('vcs.server.enable', 'true'))
202 if not vcs_server_enabled:
203 pylons_app_as_view = DisableVCSPagesWrapper(pylons_app_as_view)
204
205 def pylons_app_with_error_handler(context, request):
206 """
207 Handle exceptions from rc pylons app:
208
209 - old webob type exceptions get converted to pyramid exceptions
210 - pyramid exceptions are passed to the error handler view
211 """
212 try:
213 response = pylons_app_as_view(context, request)
214 if 400 <= response.status_int <= 599: # webob type error responses
215 return error_handler(
216 webob_to_pyramid_http_response(response), request)
217 except HTTPError as e: # pyramid type exceptions
218 return error_handler(e, request)
219 except Exception:
220 if settings.get('debugtoolbar.enabled', False):
221 raise
222 return error_handler(HTTPInternalServerError(), request)
223 return response
224
225 return pylons_app_with_error_handler
226
227
181 228 def add_pylons_compat_data(registry, global_config, settings):
182 229 """
183 230 Attach data to the registry to support the Pylons integration.
@@ -274,44 +321,11 b' def includeme(config):'
274 321 for inc in includes:
275 322 config.include(inc)
276 323
277 pylons_app = make_app(
278 config.registry._pylons_compat_global_config,
279 **config.registry._pylons_compat_settings)
280 config.registry._pylons_compat_config = pylons_app.config
281
282 pylons_app_as_view = wsgiapp(pylons_app)
283
284 # Protect from VCS Server error related pages when server is not available
285 vcs_server_enabled = asbool(settings.get('vcs.server.enable', 'true'))
286 if not vcs_server_enabled:
287 pylons_app_as_view = DisableVCSPagesWrapper(pylons_app_as_view)
288
289
290 def pylons_app_with_error_handler(context, request):
291 """
292 Handle exceptions from rc pylons app:
293
294 - old webob type exceptions get converted to pyramid exceptions
295 - pyramid exceptions are passed to the error handler view
296 """
297 try:
298 response = pylons_app_as_view(context, request)
299 if 400 <= response.status_int <= 599: # webob type error responses
300 return error_handler(
301 webob_to_pyramid_http_response(response), request)
302 except HTTPError as e: # pyramid type exceptions
303 return error_handler(e, request)
304 except Exception:
305 if settings.get('debugtoolbar.enabled', False):
306 raise
307 return error_handler(HTTPInternalServerError(), request)
308 return response
309
310 324 # This is the glue which allows us to migrate in chunks. By registering the
311 325 # pylons based application as the "Not Found" view in Pyramid, we will
312 326 # fallback to the old application each time the new one does not yet know
313 327 # how to handle a request.
314 config.add_notfound_view(pylons_app_with_error_handler)
328 config.add_notfound_view(make_not_found_view(config))
315 329
316 330 if not settings.get('debugtoolbar.enabled', False):
317 331 # if no toolbar, then any exception gets caught and rendered
General Comments 0
You need to be logged in to leave comments. Login now