]> asedeno.scripts.mit.edu Git - bluechips.git/blob - bluechips/config/middleware.py
Create Pylons app
[bluechips.git] / bluechips / config / middleware.py
1 """Pylons middleware initialization"""
2 from beaker.middleware import CacheMiddleware, 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 import config
8 from pylons.middleware import ErrorHandler, StaticJavascripts, \
9     StatusCodeRedirect
10 from pylons.wsgiapp import PylonsApp
11 from routes.middleware import RoutesMiddleware
12
13 from bluechips.config.environment import load_environment
14
15 def make_app(global_conf, full_stack=True, **app_conf):
16     """Create a Pylons WSGI application and return it
17
18     ``global_conf``
19         The inherited configuration for this application. Normally from
20         the [DEFAULT] section of the Paste ini file.
21
22     ``full_stack``
23         Whether or not this application provides a full WSGI stack (by
24         default, meaning it handles its own exceptions and errors).
25         Disable full_stack when this application is "managed" by
26         another WSGI middleware.
27
28     ``app_conf``
29         The application's local configuration. Normally specified in the
30         [app:<name>] section of the Paste ini file (where <name>
31         defaults to main).
32     """
33     # Configure the Pylons environment
34     load_environment(global_conf, app_conf)
35
36     # The Pylons WSGI app
37     app = PylonsApp()
38     
39     # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)
40     
41     # Routing/Session/Cache Middleware
42     app = RoutesMiddleware(app, config['routes.map'])
43     app = SessionMiddleware(app, config)
44     app = CacheMiddleware(app, config)
45     
46     if asbool(full_stack):
47         # Handle Python exceptions
48         app = ErrorHandler(app, global_conf, **config['pylons.errorware'])
49
50         # Display error documents for 401, 403, 404 status codes (and
51         # 500 when debug is disabled)
52         if asbool(config['debug']):
53             app = StatusCodeRedirect(app)
54         else:
55             app = StatusCodeRedirect(app, [400, 401, 403, 404, 500])
56
57     # Establish the Registry for this application
58     app = RegistryManager(app)
59
60     # Static files (If running in production, and Apache or another web 
61     # server is handling this static content, remove the following 3 lines)
62     javascripts_app = StaticJavascripts()
63     static_app = StaticURLParser(config['pylons.paths']['static_files'])
64     app = Cascade([static_app, javascripts_app, app])
65     return app