1 """Pylons middleware initialization"""
2 from beaker.middleware import SessionMiddleware
3 from paste.cascade import Cascade
4 from paste.registry import RegistryManager
5 from paste.urlparser import StaticURLParser
6 from paste.deploy.converters import asbool
7 from pylons.middleware import ErrorHandler, StatusCodeRedirect
8 from pylons.wsgiapp import PylonsApp
9 from routes.middleware import RoutesMiddleware
11 from feed.config.environment import load_environment
13 def make_app(global_conf, full_stack=True, static_files=True, **app_conf):
14 """Create a Pylons WSGI application and return it
17 The inherited configuration for this application. Normally from
18 the [DEFAULT] section of the Paste ini file.
21 Whether this application provides a full WSGI stack (by default,
22 meaning it handles its own exceptions and errors). Disable
23 full_stack when this application is "managed" by another WSGI
27 Whether this application serves its own static files; disable
28 when another web server is responsible for serving them.
31 The application's local configuration. Normally specified in
32 the [app:<name>] section of the Paste ini file (where <name>
36 # Configure the Pylons environment
37 config = load_environment(global_conf, app_conf)
40 app = PylonsApp(config=config)
42 # Routing/Session Middleware
43 app = RoutesMiddleware(app, config['routes.map'])
44 app = SessionMiddleware(app, config)
46 # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)
48 if asbool(full_stack):
49 # Handle Python exceptions
50 app = ErrorHandler(app, global_conf, **config['pylons.errorware'])
52 # Display error documents for 401, 403, 404 status codes (and
53 # 500 when debug is disabled)
54 if asbool(config['debug']):
55 app = StatusCodeRedirect(app)
57 app = StatusCodeRedirect(app, [400, 401, 403, 404, 500])
59 # Establish the Registry for this application
60 app = RegistryManager(app)
62 if asbool(static_files):
64 static_app = StaticURLParser(config['pylons.paths']['static_files'])
65 app = Cascade([static_app, app])