pvl/verkko/wsgi.py
changeset 4 b09436772d46
parent 3 5990b188c54b
child 7 7baf4cccb4a9
--- a/pvl/verkko/wsgi.py	Wed Oct 10 22:45:50 2012 +0300
+++ b/pvl/verkko/wsgi.py	Wed Oct 10 23:16:25 2012 +0300
@@ -1,12 +1,14 @@
 import werkzeug
 from werkzeug.wrappers import Request, Response
-from werkzeug.utils import redirect
+from werkzeug.exceptions import HTTPException
 
 import logging; log = logging.getLogger('pvl.verkko.wsgi')
 
-from pvl.verkko import db as database, web, hosts
+from pvl.verkko import db as database, urls, web
 
 class Application (object) :
+    urls = urls.urls
+
     def __init__ (self, db) :
         """
             Initialize app with db.
@@ -14,31 +16,32 @@
 
         self.db = database.Database(db)
 
+    def respond (self, request) :
+        """
+            Lookup Request -> web.Handler, params
+        """
+        
+        # bind to request
+        urls = self.urls.bind_to_environ(request)
+        
+        # lookup
+        handler, params = urls.match()
+
+        # handler instance
+        handler = handler(self, request, urls)
+
+        # apply
+        return handler.respond(**params)
+
     @Request.application
     def __call__ (self, request) :
         """
             WSGI entry point, werkzeug Request -> Response
         """
-        
-        # path?
-        path = request.path.strip('/')
-        
-        if path :
-            path = path.split('/')
-        else :
-            path = []
 
-        log.debug("path: %s", path)
+        try :
+            return self.respond(request)
         
-        # lookup handler/respond
-        if not path :
-            handler = web.Index
+        except HTTPException as ex :
+            return ex
 
-        elif path[0] == 'hosts' :
-            handler = hosts.Handler
-            path.pop(0)
-
-        else :
-            return Response("Not Found", status=404)
-        
-        return handler(self, request, path).respond()