| 1 |
from litestar import Litestar, get |
| 2 |
from litestar.connection import ASGIConnection |
| 3 |
from litestar.exceptions import NotAuthorizedException |
| 4 |
from litestar.handlers import BaseRouteHandler |
| 5 |
|
| 6 |
|
| 7 |
async def require_token(connection: ASGIConnection, route_handler: BaseRouteHandler) -> None: |
| 8 |
if connection.headers.get("authorization") != "Bearer secret": |
| 9 |
raise NotAuthorizedException |
| 10 |
|
| 11 |
|
| 12 |
@get("/", guards=[require_token]) |
| 13 |
async def index() -> dict[str, str]: |
| 14 |
return {"hello": "world"} |
| 15 |
|
| 16 |
|
| 17 |
app = Litestar(route_handlers=[index]) |