docs/examples/application_state/using_custom_state.py
raw| 1 | from typing import Any, Dict |
| 2 | |
| 3 | from litestar import Litestar, get |
| 4 | from litestar.datastructures import State |
| 5 | |
| 6 | |
| 7 | class MyState(State): |
| 8 | count: int = 0 |
| 9 | |
| 10 | def increment(self) -> None: |
| 11 | self.count += 1 |
| 12 | |
| 13 | |
| 14 | @get("/", sync_to_thread=False) |
| 15 | def handler(state: MyState) -> Dict[str, Any]: |
| 16 | state.increment() |
| 17 | return state.dict() |
| 18 | |
| 19 | |
| 20 | app = Litestar(route_handlers=[handler]) |