]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Add a FallbackHandler class to allow use of other applications (e.g.
authorBen Darnell <bdarnell@beaker.local>
Thu, 3 Dec 2009 19:57:52 +0000 (11:57 -0800)
committerBen Darnell <bdarnell@beaker.local>
Thu, 3 Dec 2009 19:57:52 +0000 (11:57 -0800)
tornado.wsgi.WSGIContainer) from within a tornado.web.Application to facilitate
migration from other web frameworks.

tornado/web.py

index d77f0c566835b518ec666cef094419bf597a66f3..7b8dd7d0947a49ac30947e031a5193f578b18df2 100644 (file)
@@ -1084,6 +1084,29 @@ class StaticFileHandler(RequestHandler):
             file.close()
 
 
+class FallbackHandler(RequestHandler):
+    """A RequestHandler that wraps another HTTP server callback.
+
+    The fallback is a callable object that accepts an HTTPRequest,
+    such as an Application or tornado.wsgi.WSGIContainer.  This is most
+    useful to use both tornado RequestHandlers and WSGI in the same server.
+    Typical usage:
+        wsgi_app = tornado.wsgi.WSGIContainer(
+            django.core.handlers.wsgi.WSGIHandler())
+        application = tornado.web.Application([
+            (r"/foo", FooHandler),
+            (r".*", FallbackHandler, dict(fallback=wsgi_app),
+        ])
+    """
+    def __init__(self, app, request, fallback):
+        RequestHandler.__init__(self, app, request)
+        self.fallback = fallback
+
+    def prepare(self):
+        self.fallback(self.request)
+        self._finished = True
+
+
 class OutputTransform(object):
     """A transform modifies the result of an HTTP request (e.g., GZip encoding)