annotate pylons_app/config/middleware.py @ 10:525ed90e4577

major app speedup moved the wsgi creation to app globals, in order to make it run only once. little config changes.
author Marcin Kuzminski
date Sat, 20 Feb 2010 14:30:13 +0100
parents 7322f231ad09
children 5f30a6d558dc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
1 """Pylons middleware initialization"""
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
2 from beaker.middleware import CacheMiddleware, SessionMiddleware
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
3 from paste.cascade import Cascade
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
4 from paste.registry import RegistryManager
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
5 from paste.urlparser import StaticURLParser
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
6 from paste.deploy.converters import asbool
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
7 from pylons import config
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
8 from pylons.middleware import ErrorHandler, StatusCodeRedirect
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
9 from pylons.wsgiapp import PylonsApp
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
10 from routes.middleware import RoutesMiddleware
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
11
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
12 from pylons_app.config.environment import load_environment
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
13
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
14 def make_app(global_conf, full_stack = True, static_files = True, **app_conf):
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
15 """Create a Pylons WSGI application and return it
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
16
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
17 ``global_conf``
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
18 The inherited configuration for this application. Normally from
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
19 the [DEFAULT] section of the Paste ini file.
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
20
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
21 ``full_stack``
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
22 Whether this application provides a full WSGI stack (by default,
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
23 meaning it handles its own exceptions and errors). Disable
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
24 full_stack when this application is "managed" by another WSGI
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
25 middleware.
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
26
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
27 ``static_files``
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
28 Whether this application serves its own static files; disable
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
29 when another web server is responsible for serving them.
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
30
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
31 ``app_conf``
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
32 The application's local configuration. Normally specified in
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
33 the [app:<name>] section of the Paste ini file (where <name>
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
34 defaults to main).
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
35
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
36 """
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
37 # Configure the Pylons environment
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
38 load_environment(global_conf, app_conf)
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
39
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
40 # The Pylons WSGI app
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
41 app = PylonsApp()
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
42
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
43 # Routing/Session/Cache Middleware
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
44 app = RoutesMiddleware(app, config['routes.map'])
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
45 app = SessionMiddleware(app, config)
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
46 app = CacheMiddleware(app, config)
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
47
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
48 # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
49
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
50 if asbool(full_stack):
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
51 # Handle Python exceptions
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
52 app = ErrorHandler(app, global_conf, **config['pylons.errorware'])
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
53
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
54 # Display error documents for 401, 403, 404 status codes (and
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
55 # 500 when debug is disabled)
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
56 if asbool(config['debug']):
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
57 app = StatusCodeRedirect(app)
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
58 else:
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
59 app = StatusCodeRedirect(app, [400, 401, 403, 404, 500])
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
60
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
61 # Establish the Registry for this application
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
62 app = RegistryManager(app)
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
63
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
64 if asbool(static_files):
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
65 # Serve static files
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
66 static_app = StaticURLParser(config['pylons.paths']['static_files'])
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
67 app = Cascade([static_app, app])
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
68
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
69 #dozer debug
10
525ed90e4577 major app speedup moved the wsgi creation to app globals, in order to make it run only once.
Marcin Kuzminski
parents: 1
diff changeset
70 if asbool(config['debug']):
525ed90e4577 major app speedup moved the wsgi creation to app globals, in order to make it run only once.
Marcin Kuzminski
parents: 1
diff changeset
71 from dozer import Logview
525ed90e4577 major app speedup moved the wsgi creation to app globals, in order to make it run only once.
Marcin Kuzminski
parents: 1
diff changeset
72 app = Logview(app, config)
0
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
73
564e40829f80 initial commit.
Marcin Kuzminski
parents:
diff changeset
74 return app