import os
import logging
-from mediagoblin.routing import url_map, view_functions, add_route
+from mediagoblin.routing import get_url_map
+from mediagoblin.tools.routing import endpoint_to_controller
from werkzeug.wrappers import Request
-from werkzeug.exceptions import HTTPException, NotFound
+from werkzeug.exceptions import HTTPException
+from werkzeug.routing import RequestRedirect
from mediagoblin import meddleware, __version__
from mediagoblin.tools import common, translate, template
-from mediagoblin.tools.response import render_404
+from mediagoblin.tools.response import render_http_exception
from mediagoblin.tools.theme import register_themes
from mediagoblin.tools import request as mg_request
from mediagoblin.mg_globals import setup_globals
setup_plugins()
# Set up the database
- self.connection, self.db = setup_database()
+ self.db = setup_database()
# Register themes
self.theme_registry, self.current_theme = register_themes(app_config)
self.public_store, self.queue_store = setup_storage()
# set up routing
- self.url_map = url_map
-
- for route in PluginManager().get_routes():
- add_route(*route)
+ self.url_map = get_url_map()
# set up staticdirector tool
self.staticdirector = get_staticdirector(app_config)
mg_request.setup_user_in_request(request)
try:
- endpoint, url_values = map_adapter.match()
+ found_rule, url_values = map_adapter.match(return_rule=True)
request.matchdict = url_values
- except NotFound as exc:
- return render_404(request)(environ, start_response)
+ except RequestRedirect as response:
+ # Deal with 301 responses eg due to missing final slash
+ return response(environ, start_response)
except HTTPException as exc:
- # exceptions that match() is documented to return:
- # MethodNotAllowed, RequestRedirect TODO: need to handle ???
- return exc(environ, start_response)
-
- view_func = view_functions[endpoint]
+ # Stop and render exception
+ return render_http_exception(
+ request, exc,
+ exc.get_description(environ))(environ, start_response)
- _log.debug('endpoint: {0} view_func: {1}'.format(
- endpoint,
- view_func))
-
- # import the endpoint, or if it's already a callable, call that
- if isinstance(view_func, unicode) \
- or isinstance(view_func, str):
- controller = common.import_component(view_func)
- else:
- controller = view_func
+ controller = endpoint_to_controller(found_rule)
# pass the request through our meddleware classes
- for m in self.meddleware:
- response = m.process_request(request, controller)
- if response is not None:
- return response(environ, start_response)
+ try:
+ for m in self.meddleware:
+ response = m.process_request(request, controller)
+ if response is not None:
+ return response(environ, start_response)
+ except HTTPException as e:
+ return render_http_exception(
+ request, e,
+ e.get_description(environ))(environ, start_response)
request.start_response = start_response
- # get the response from the controller
- response = controller(request)
+ # get the Http response from the controller
+ try:
+ response = controller(request)
+ except HTTPException as e:
+ response = render_http_exception(
+ request, e, e.get_description(environ))
- # pass the response through the meddleware
- for m in self.meddleware[::-1]:
- m.process_response(request, response)
+ # pass the response through the meddlewares
+ try:
+ for m in self.meddleware[::-1]:
+ m.process_response(request, response)
+ except HTTPException as e:
+ response = render_http_exeption(
+ request, e, e.get_description(environ))
return response(environ, start_response)
mgoblin_app = MediaGoblinApp(mediagoblin_config)
+ for callable_hook in PluginManager().get_hook_callables('wrap_wsgi'):
+ mgoblin_app = callable_hook(mgoblin_app)
+
return mgoblin_app