python-dependency-injector/examples/miniapps/fastapi-simple/fastapi_di_example.py

28 lines
562 B
Python
Raw Permalink Normal View History

2020-11-18 19:13:31 +03:00
from fastapi import FastAPI, Depends
from dependency_injector import containers, providers
2021-10-01 02:08:49 +03:00
from dependency_injector.wiring import Provide, inject
2020-11-18 19:13:31 +03:00
class Service:
async def process(self) -> str:
2021-10-01 02:08:49 +03:00
return "OK"
2020-11-18 19:13:31 +03:00
class Container(containers.DeclarativeContainer):
service = providers.Factory(Service)
app = FastAPI()
2021-10-01 02:08:49 +03:00
@app.api_route("/")
2020-11-18 19:13:31 +03:00
@inject
async def index(service: Service = Depends(Provide[Container.service])):
result = await service.process()
2021-10-01 02:08:49 +03:00
return {"result": result}
2020-11-18 19:13:31 +03:00
container = Container()
2021-10-01 02:08:49 +03:00
container.wire(modules=[__name__])