| 1 |
from typing import Dict |
| 2 |
|
| 3 |
from litestar import Controller, Litestar, get |
| 4 |
from litestar.connection import ASGIConnection |
| 5 |
from litestar.exceptions import NotAuthorizedException |
| 6 |
from litestar.handlers import BaseRouteHandler |
| 7 |
|
| 8 |
|
| 9 |
def require_admin(connection: ASGIConnection, route_handler: BaseRouteHandler) -> None: |
| 10 |
if connection.headers.get("x-role") != "admin": |
| 11 |
raise NotAuthorizedException(detail="admin only") |
| 12 |
|
| 13 |
|
| 14 |
class AdminController(Controller): |
| 15 |
path = "/admin" |
| 16 |
guards = [require_admin] |
| 17 |
|
| 18 |
@get("/stats") |
| 19 |
async def stats(self) -> Dict[str, int]: |
| 20 |
return {"users": 100} |
| 21 |
|
| 22 |
|
| 23 |
app = Litestar(route_handlers=[AdminController]) |