changeset 4:dd1c4916cbcd

[mq]: fileserver
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 07 Sep 2010 22:39:15 -0700
parents 56ab6b90cd1a
children b2fbb4f982da
files simplewiki/dispatcher.py simplewiki/handlers.py
diffstat 2 files changed, 16 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/simplewiki/dispatcher.py	Tue Sep 07 21:29:57 2010 -0700
+++ b/simplewiki/dispatcher.py	Tue Sep 07 22:39:15 2010 -0700
@@ -5,7 +5,7 @@
 
 import os
 
-from handlers import GenshiRenderer, Index, Post
+from handlers import GenshiRenderer, Index, Post, FileServer
 
 from genshi.template import TemplateLoader
 from paste.fileapp import FileApp
@@ -30,7 +30,7 @@
         assert self.directory and os.path.exists(self.directory), "Must specify an existing directory"
 
         # request handlers
-        self.handlers = [ Post, GenshiRenderer, Index ]
+        self.handlers = [ Post, GenshiRenderer, Index, FileServer ]
 
         # template loader
         self.template_dirs = self.template_dirs.split()
@@ -57,13 +57,6 @@
         else:
             handler = exc.HTTPNotFound
 
-        # add navigation links to handler [example]
-        if hasattr(handler, 'data'):
-            handler.data.setdefault('links', [])
-            for h in self.handlers:
-                handler.data['links'].append((handler.link(h.handler_path), 
-                                              h.__name__))
-
         # get response
         res = handler()
         return res(environ, start_response)
--- a/simplewiki/handlers.py	Tue Sep 07 21:29:57 2010 -0700
+++ b/simplewiki/handlers.py	Tue Sep 07 22:39:15 2010 -0700
@@ -4,6 +4,7 @@
 """
 
 import os
+from paste.fileapp import FileApp 
 from urlparse import urlparse
 from webob import Response, exc
 
@@ -142,3 +143,16 @@
         
     def __call__(self):
         self.redirect(self.location)
+
+class FileServer(Handler):
+    methods = set(['GET']) # methods to listen to
+
+    def __init__(self, app, request):
+        Handler.__init__(self, app, request)
+        self.file = os.path.join(self.app.directory, *request.environ['path'])
+        if not os.path.exists(self.file):
+            raise HandlerMatchException
+
+    def __call__(self):
+        return FileApp(self.file)
+