lib/wsgi.py
branchsites
changeset 31 107062ebb6f9
parent 30 a86a25a9f75b
--- a/lib/wsgi.py	Sat Feb 07 06:54:52 2009 +0200
+++ b/lib/wsgi.py	Sat Feb 07 16:33:27 2009 +0200
@@ -9,63 +9,60 @@
 # for Request/Response
 import http
 
-# to lookup the Site
-from site import lookup as site_lookup
-
-# for the request -> response bit :)
-import handler
-
-def request_handler (env, start_response) :
+class Application (object) :
     """
-        The actual request handling code
+        Our WSGI application, implements the wsgi __call__ interface
     """
 
-    # build Request object
-    request = http.Request(env)
-
-    # lookup site
-    site = site_lookup(request)
-
-    # mapper...
-    mapper = site.get_mapper()
+    def __init__ (self, handler) :
+        """
+            Initialize to use the given handler for requests
+        """
 
-    # lookup handler
-    handler = mapper.map_request(request)
-    
-    try :
-        # request -> response
-        response = handler.handle_request(request)
-
-    except http.ResponseError, err :
-        # just use the generated response
-        response = err.get_response()
-
-    # send response
-    assert response, "No response"
+        self.handler = handler
     
-    # send response status/headers
-    start_response(response.get_status(), response.get_headers())
-
-    # send respones data
-    yield response.get_data()
-
-def app (env, start_response) :
-    """
-        Wraps request_handler to trap errors
-    """
+    def handle_request (self, env, start_response) :
+        """
+            The actual request handling code
+        """
 
-    try :
-        # passthrough request_handler
-        for chunk in request_handler(env, start_response) :
-            yield chunk
+        # build Request object
+        request = http.Request(env)
 
-    except :
-        # execption info
-        info = sys.exc_info()
+        try :
+            # request -> response using our handler
+            response = self.handler.handle_request(request)
 
-        # try and send 500 ISE to browser, if no headers yet...
-        start_response("500 Internal Server Error", [('Content-type', "text/plain; charset=utf8")], info)
+        except http.ResponseError, err :
+            # just use the generated response
+            response = err.get_response()
 
-        # send traceback
-        yield traceback.format_exc()
+        # send response
+        assert response, "No response"
+        
+        # send response status/headers
+        start_response(response.get_status(), response.get_headers())
 
+        # send respones data
+        yield response.get_data()
+
+    def __call__ (self, env, start_response) :
+        """
+            Wraps handle_request to trap errors
+        """
+
+        try :
+            # passthrough request_handler
+            for chunk in self.handle_request(env, start_response) :
+                yield chunk
+
+        except :
+            # execption info
+            info = sys.exc_info()
+
+            # try and send 500 ISE to browser, if no headers yet...
+            start_response("500 Internal Server Error", [('Content-type', "text/plain; charset=utf8")], info)
+
+            # send traceback
+            yield traceback.format_exc()
+