我遇到过一个场景,我必须重写 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_response
是在配置阶段调用 make_pylons_stack
时传入。如果我想覆盖 TrackingMiddleware
我应该在 ckanext-myext/
下创建另一个 config/middleware/myTrackingMiddleware.py
然后在 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 。在 dev.ini
中禁用 CKAN ckan.tracking_enabled
后,我可以灵活地从 environ
获取值并使用自定义逻辑处理跟踪。
关于python - 如何在CKAN中制作定制中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52230555/