我试图弄清楚如何使配置/单例可用于不同的模块。也许有一种我还不知道的标准 Python 方法。所以我创建了一个包含所有应用程序配置的配置单例,并希望与所有模块“共享”它。相同的用例将适用于共享数据库连接。
主文件
app = FastApi()
config = some_config_object_from_somewhere()
app.include_router(
collection.router,
prefix='/api/collection'
)
api/collection.py
router = APIRouter()
@router.post("/", status_code=201)
async def collect():
# I want to use config that is created/defined in main.py
# HOW? I thought dependency injection that is built into FastAPI would
# help, but can't seem to define something in a different module and have it
# available in the 'router' module