docs/examples/onboarding/django/state.py
raw| 1 | from litestar import Litestar, get |
| 2 | from litestar.datastructures import State |
| 3 | |
| 4 | |
| 5 | class RedisClient: |
| 6 | """Stand-in for a configured Redis client.""" |
| 7 | |
| 8 | |
| 9 | @get("/") |
| 10 | async def handler(state: State) -> dict[str, str]: |
| 11 | redis: RedisClient = state.redis |
| 12 | return {"client": type(redis).__name__} |
| 13 | |
| 14 | |
| 15 | app = Litestar( |
| 16 | route_handlers=[handler], |
| 17 | state=State({"redis": RedisClient()}), |
| 18 | ) |