diff --git a/kallithea/config/middleware.py b/kallithea/config/application.py copy from kallithea/config/middleware.py copy to kallithea/config/application.py --- a/kallithea/config/middleware.py +++ b/kallithea/config/application.py @@ -14,26 +14,46 @@ """WSGI middleware initialization for the Kallithea application.""" from kallithea.config.app_cfg import base_config -from kallithea.config.environment import load_environment +from kallithea.config.middleware.https_fixup import HttpsFixup +from kallithea.config.middleware.permanent_repo_url import PermanentRepoUrl +from kallithea.config.middleware.simplegit import SimpleGit +from kallithea.config.middleware.simplehg import SimpleHg +from kallithea.config.middleware.wrapper import RequestWrapper +from kallithea.lib.utils2 import asbool __all__ = ['make_app'] -# Use base_config to setup the necessary PasteDeploy application factory. -# make_base_app will wrap the TurboGears2 app with all the middleware it needs. -make_base_app = base_config.setup_tg_wsgi_app(load_environment) + +def wrap_app(app): + """Wrap the TG WSGI application in Kallithea middleware""" + config = app.config + + # we want our low level middleware to get to the request ASAP. We don't + # need any stack middleware in them - especially no StatusCodeRedirect buffering + app = SimpleHg(app, config) + app = SimpleGit(app, config) + + # Enable https redirects based on HTTP_X_URL_SCHEME set by proxy + if any(asbool(config.get(x)) for x in ['url_scheme_variable', 'force_https', 'use_htsts']): + app = HttpsFixup(app, config) + + app = PermanentRepoUrl(app, config) + + # Optional and undocumented wrapper - gives more verbose request/response logging, but has a slight overhead + if asbool(config.get('use_wsgi_wrapper')): + app = RequestWrapper(app, config) + + return app -def make_app(global_conf, full_stack=True, **app_conf): +def make_app(global_conf, **app_conf): """ Set up Kallithea with the settings found in the PasteDeploy configuration file used. :param global_conf: The global settings for Kallithea (those defined under the ``[DEFAULT]`` section). - :type global_conf: dict - :param full_stack: Should the whole TurboGears2 stack be set up? - :type full_stack: str or bool :return: The Kallithea application with all the relevant middleware loaded. @@ -44,4 +64,5 @@ def make_app(global_conf, full_stack=Tru """ assert app_conf.get('sqlalchemy.url') # must be called with a Kallithea .ini file, which for example must have this config option assert global_conf.get('here') and global_conf.get('__file__') # app config should be initialized the paste way ... - return make_base_app(global_conf, full_stack=full_stack, **app_conf) + + return base_config.make_wsgi_app(global_conf, app_conf, wrap_app=wrap_app)