首页 > 解决方案 > 如何在 Sanic 中使用依赖注入实现 CORS

问题描述

我正在使用依赖注入库在 Sanic 中构建 API,并且正在尝试实现 CORS。

我试图将 Sanic 类分配给一个变量并将其CORS()传递__name__providers.Factory.

下面的评论显示了我尝试实现 CORS 的确切行:

from sanic_cors import CORS, cross_origin # imported the Sanic_cors here

class Application(containers.DeclarativeContainer):
    """
    Application components container
    """
  # declared the app variable here and passed the Sanic class
    app = Sanic()
  # passed the variable in to CORS()
    CORS(app) 
    webapi = providers.Factory(app, __name__)

我预计运行 API 不会出错,但我在下面收到此错误:

Traceback (most recent call last):
  File "api.py", line 7, in <module>
    run_api(APP)
  File "/Users/Kassim/Documents/Projects/pythonProjects/my-app-engine/app/api_runner.py", line 8, in run_api
    web_api = application.webapi()
  File "src/dependency_injector/providers.pyx", line 100, in dependency_injector.providers.Provider.__call__
  File "src/dependency_injector/providers.pyx", line 1247, in dependency_injector.providers.Factory._provide
  File "src/dependency_injector/providers.pxd", line 278, in dependency_injector.providers.__factory_call
  File "src/dependency_injector/providers.pxd", line 272, in dependency_injector.providers.__callable_call
TypeError: __call__() takes 1 positional argument but 2 were given

标签: python-3.xsanic

解决方案


推荐阅读