# HG changeset patch # User Mads Kiilerich # Date 2020-04-24 13:32:17 # Node ID d757635af3c20cdcfeb65af8f46045706ffa6f46 # Parent 27d9ca0c83815178bb647055436e59383e7a68ea tg: include the Kallithea middleware wrapping of the TG WSGI application in application.py This seems to make the architecture more clear than using hooks. diff --git a/kallithea/config/app_cfg.py b/kallithea/config/app_cfg.py --- a/kallithea/config/app_cfg.py +++ b/kallithea/config/app_cfg.py @@ -34,11 +34,6 @@ import kallithea.lib.locale import kallithea.model.base import kallithea.model.meta from kallithea.lib import celerypylons -from kallithea.lib.middleware.https_fixup import HttpsFixup -from kallithea.lib.middleware.permanent_repo_url import PermanentRepoUrl -from kallithea.lib.middleware.simplegit import SimpleGit -from kallithea.lib.middleware.simplehg import SimpleHg -from kallithea.lib.middleware.wrapper import RequestWrapper from kallithea.lib.utils import check_git_version, load_rcextensions, set_app_settings, set_indexer_config, set_vcs_config from kallithea.lib.utils2 import asbool from kallithea.model import db @@ -168,27 +163,3 @@ def setup_configuration(app): tg.hooks.register('configure_new_app', setup_configuration) - - -def setup_application(app): - 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 ['https_fixup', '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 - - -tg.hooks.register('before_wsgi_middlewares', setup_application) diff --git a/kallithea/config/application.py b/kallithea/config/application.py --- a/kallithea/config/application.py +++ b/kallithea/config/application.py @@ -14,11 +14,39 @@ """WSGI middleware initialization for the Kallithea application.""" from kallithea.config.app_cfg import base_config +from kallithea.lib.middleware.https_fixup import HttpsFixup +from kallithea.lib.middleware.permanent_repo_url import PermanentRepoUrl +from kallithea.lib.middleware.simplegit import SimpleGit +from kallithea.lib.middleware.simplehg import SimpleHg +from kallithea.lib.middleware.wrapper import RequestWrapper +from kallithea.lib.utils2 import asbool __all__ = ['make_app'] +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 ['https_fixup', '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, **app_conf): """ Set up Kallithea with the settings found in the PasteDeploy configuration @@ -37,4 +65,4 @@ def make_app(global_conf, **app_conf): 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 base_config.make_wsgi_app(global_conf, app_conf, wrap_app=None) + return base_config.make_wsgi_app(global_conf, app_conf, wrap_app=wrap_app)