replace nested middleware calls with reduce and partials

This commit is contained in:
Martin Zimmermann 2013-11-25 13:38:51 +01:00
parent c9c0df229a
commit 6405f258f3

View File

@ -46,6 +46,7 @@ import tempfile
from os.path import dirname, join from os.path import dirname, join
from argparse import ArgumentParser from argparse import ArgumentParser
from functools import partial, reduce
from itsdangerous import URLSafeTimedSerializer from itsdangerous import URLSafeTimedSerializer
@ -56,6 +57,7 @@ from werkzeug.wsgi import SharedDataMiddleware
from werkzeug.local import Local, LocalManager from werkzeug.local import Local, LocalManager
from werkzeug.serving import run_simple from werkzeug.serving import run_simple
from werkzeug.contrib.fixers import ProxyFix from werkzeug.contrib.fixers import ProxyFix
from werkzeug.contrib.profiler import ProfilerMiddleware
local = Local() local = Local()
local_manager = LocalManager([local]) local_manager = LocalManager([local])
@ -158,23 +160,22 @@ def make_app(conf=None):
else: else:
logger.warn("unable to connect to HTTP server") logger.warn("unable to connect to HTTP server")
wrapper = [local_manager.make_middleware]
if isso.conf.getboolean("server", "profile"): if isso.conf.getboolean("server", "profile"):
from werkzeug.contrib.profiler import ProfilerMiddleware as Profiler wrapper.append(partial(ProfilerMiddleware,
ProfilerMiddleware = lambda app: Profiler(app, sort_by=("cumtime", ), restrictions=("isso/(?!lib)", 10)) sort_by=("cumtime", ), restrictions=("isso/(?!lib)", 10)))
else:
ProfilerMiddleware = lambda app: app
app = ProxyFix( wrapper.append(partial(SharedDataMiddleware, exports={
wsgi.SubURI(
wsgi.CORSMiddleware(
SharedDataMiddleware(
ProfilerMiddleware(
local_manager.make_middleware(isso)), {
'/js': join(dirname(__file__), 'js/'), '/js': join(dirname(__file__), 'js/'),
'/css': join(dirname(__file__), 'css/')}), '/css': join(dirname(__file__), 'css/')}))
origin(isso.conf.getiter("general", "host")))))
return app wrapper.append(partial(wsgi.CORSMiddleware,
origin=origin(isso.conf.getiter("general", "host"))))
wrapper.extend([wsgi.SubURI, ProxyFix])
return reduce(lambda x, f: f(x), wrapper, isso)
def main(): def main():