0
|
1 import os
|
|
2
|
|
3 from dispatcher import Dispatcher
|
|
4 from paste.httpexceptions import HTTPExceptionHandler
|
|
5 from paste.urlparser import StaticURLParser
|
|
6 from pkg_resources import resource_filename
|
|
7
|
|
8 class PassthroughFileserver(object):
|
|
9 """serve files if they exist"""
|
|
10
|
|
11 def __init__(self, app, directory):
|
|
12 self.app = app
|
|
13 self.directory = directory
|
|
14 self.fileserver = StaticURLParser(self.directory)
|
|
15
|
|
16 def __call__(self, environ, start_response):
|
|
17 path = environ['PATH_INFO'].strip('/')
|
|
18 if path and os.path.exists(os.path.join(self.directory, path)):
|
|
19 return self.fileserver(environ, start_response)
|
|
20 return self.app(environ, start_response)
|
|
21
|
1
|
22 def factory(**app_conf):
|
0
|
23 """create a webob view and wrap it in middleware"""
|
1
|
24 app = Dispatcher(**app_conf)
|
|
25 return HTTPExceptionHandler(PassthroughFileserver(app, resource_filename(__name__, 'static')))
|
0
|
26
|
1
|
27 if __name__ == '__main__':
|
2
|
28 import shutil
|
1
|
29 import tempfile
|
|
30 from wsgiref import simple_server
|
2
|
31 tempdir = tempfile.mkdtemp()
|
|
32 try:
|
|
33 app = factory
|
|
34 finally:
|
|
35 shutil.rmtree(tempdir)
|