mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-25 11:04:01 +03:00
28 lines
562 B
Python
28 lines
562 B
Python
from fastapi import FastAPI, Depends
|
|
from dependency_injector import containers, providers
|
|
from dependency_injector.wiring import Provide, inject
|
|
|
|
|
|
class Service:
|
|
async def process(self) -> str:
|
|
return "OK"
|
|
|
|
|
|
class Container(containers.DeclarativeContainer):
|
|
|
|
service = providers.Factory(Service)
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
@app.api_route("/")
|
|
@inject
|
|
async def index(service: Service = Depends(Provide[Container.service])):
|
|
result = await service.process()
|
|
return {"result": result}
|
|
|
|
|
|
container = Container()
|
|
container.wire(modules=[__name__])
|