我遇到了一个方案,我必须重写CKAN中的通用中间件。在CKAN中,默认plugins/interface.py

class IMiddleware(Interface):
    u'''Hook into CKAN middleware stack
    Note that methods on this interface will be called two times,
    one for the Pylons stack and one for the Flask stack (eventually
    there will be only the Flask stack).
    '''
    def make_middleware(self, app, config):
        u'''Return an app configured with this middleware
        When called on the Flask stack, this method will get the actual Flask
        app so plugins wanting to install Flask extensions can do it like
        this::
            import ckan.plugins as p
            from flask_mail import Mail
            class MyPlugin(p.SingletonPlugin):
                p.implements(p.I18nMiddleware)
                def make_middleware(app, config):
                    mail = Mail(app)
                    return app
        '''
        return app


它表明我必须在要在扩展中实现的插件下定义“ MyMiddleWare”类。但是,如示例所示,实际的中间件Mail是从其他类导入的。我想覆盖TrackingMiddleware特别是__call__(self, environ, start_response)方法,在配置阶段调用environ时将传递start_responsemake_pylons_stack。如果要覆盖TrackingMiddleware是否应在config/middleware/myTrackingMiddleware.py下创建另一个ckanext-myext/,然后在plugin.py中实现以下内容?:

from myTrackingMiddleware import MyTrackingMiddleware

class MyPlugin(plugins.SingletonPlugin):
    plugins.implements(plugins.IMiddleware, inherit=True)

    def make_middleware(self, app, config):
        tracking = MytrackingMiddleware(app, config)
        return app


更新:

我试图在层次结构中创建myTrackingMiddleware并将其导入到plugin.py中,但是在'/_tracking'中没有收到对myTrackingMiddleware的任何请求。

最佳答案

我已经实施了一套过程,并且对我自己有效。基本上,我保持自己所做的事情与我在自己的问题中提到的一样。然后,如果中间件与CKAN默认中间件有一些冲突,则可能必须完全禁用默认中间件。我在这里与CKAN的一些主要贡献者进行了讨论:https://github.com/ckan/ckan/issues/4451。在ckan.tracking_enabled中禁用CKAN dev.ini后,我可以灵活地从environ获取值并使用自定义逻辑处理跟踪。

关于python - 如何在CKAN中制作自定义中间件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52230555/

10-12 22:11