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, StatusCodeRedirect |
---|
9 | from pylons.wsgiapp import PylonsApp |
---|
10 | from routes.middleware import RoutesMiddleware |
---|
11 | |
---|
12 | from openidaxtest.config.environment import load_environment |
---|
13 | import authkit.authenticate |
---|
14 | |
---|
15 | def make_app(global_conf, full_stack=True, static_files=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 this application provides a full WSGI stack (by default, |
---|
24 | meaning it handles its own exceptions and errors). Disable |
---|
25 | full_stack when this application is "managed" by another WSGI |
---|
26 | middleware. |
---|
27 | |
---|
28 | ``static_files`` |
---|
29 | Whether this application serves its own static files; disable |
---|
30 | when another web server is responsible for serving them. |
---|
31 | |
---|
32 | ``app_conf`` |
---|
33 | The application's local configuration. Normally specified in |
---|
34 | the [app:<name>] section of the Paste ini file (where <name> |
---|
35 | defaults to main). |
---|
36 | |
---|
37 | """ |
---|
38 | # Configure the Pylons environment |
---|
39 | load_environment(global_conf, app_conf) |
---|
40 | |
---|
41 | # The Pylons WSGI app |
---|
42 | app = PylonsApp() |
---|
43 | |
---|
44 | # Routing/Session/Cache Middleware |
---|
45 | app = RoutesMiddleware(app, config['routes.map']) |
---|
46 | app = authkit.authenticate.middleware(app, app_conf) |
---|
47 | app = SessionMiddleware(app, config) |
---|
48 | app = CacheMiddleware(app, config) |
---|
49 | |
---|
50 | # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares) |
---|
51 | |
---|
52 | if asbool(full_stack): |
---|
53 | # Handle Python exceptions |
---|
54 | app = ErrorHandler(app, global_conf, **config['pylons.errorware']) |
---|
55 | |
---|
56 | # Display error documents for 401, 403, 404 status codes (and |
---|
57 | # 500 when debug is disabled) |
---|
58 | if asbool(config['debug']): |
---|
59 | app = StatusCodeRedirect(app) |
---|
60 | else: |
---|
61 | app = StatusCodeRedirect(app, [400, 401, 403, 404, 500]) |
---|
62 | |
---|
63 | # Establish the Registry for this application |
---|
64 | app = RegistryManager(app) |
---|
65 | |
---|
66 | if asbool(static_files): |
---|
67 | # Serve static files |
---|
68 | static_app = StaticURLParser(config['pylons.paths']['static_files']) |
---|
69 | app = Cascade([static_app, app]) |
---|
70 | |
---|
71 | return app |
---|