Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Ticket #559: wsgi.2.py

Line 
1 # This should work just like Tool, copying also the middleware args to make
2 # them easily introspectable, all args configured for the registered middleware
3 # are passed as keyword arguments to __call__ to configure the middleware
4 # FIXME this should be moved inside CP like Tool
5 class Middleware(object):
6     def __init__(self, middleware):
7         self.middleware = middleware
8
9     def __call__(self, **conf):
10         return lambda app: self.middleware(app, **conf)
11
12 class ChangeCase(object):
13     def __init__(self, app, to=None):
14         self.app = app
15         self.to = to
16
17     def __call__(self, environ, start_response):
18         response_iter = self.app(environ, start_response)
19         response_string = ''.join(response_iter)
20         if self.to == "upper":
21             return [response_string.upper()]
22         elif self.to == "lower":
23             return [response_string.lower()]
24         else:
25             return [response_string]
26
27 import cherrypy
28
29 class Root(object):
30     def index(self):
31         dumb = 50
32         return "HeLlO WoRlD!"
33     index.exposed = True
34
35 global_conf = {
36     'log.screen': True,
37     'request.throw_errors': True,
38 }
39
40 # configure the middleware pipeline and our middleware
41 # TODO fix the namespace, wsgi? middleware? wsgi.middleware.*?
42 app_conf = {
43     '/': {
44         'wsgi.middleware_pipeline': ['changecase'],
45         'wsgi.changecase.to': 'lower',
46     }
47 }
48
49 # register our middleware like you do with a tool
50 cherrypy.wsgi.changecase = Middleware(ChangeCase)
51
52 cherrypy.config.update(global_conf)
53 cherrypy.quickstart(Root(), script_name='/', conf=app_conf)

Hosted by WebFaction

Log in as guest/cpguest to create tickets