| 1 |
from typing import Dict |
| 2 |
|
| 3 |
from litestar import Litestar, MediaType, Response, get |
| 4 |
|
| 5 |
|
| 6 |
async def after_request(response: Response) -> Response: |
| 7 |
if response.media_type == MediaType.TEXT: |
| 8 |
return Response({"message": response.content}) |
| 9 |
return response |
| 10 |
|
| 11 |
|
| 12 |
@get("/hello") |
| 13 |
async def hello() -> str: |
| 14 |
return "Hello, world" |
| 15 |
|
| 16 |
|
| 17 |
@get("/goodbye") |
| 18 |
async def goodbye() -> Dict[str, str]: |
| 19 |
return {"message": "Goodbye"} |
| 20 |
|
| 21 |
|
| 22 |
app = Litestar(route_handlers=[hello, goodbye], after_request=after_request) |
| 23 |
|
| 24 |
|
| 25 |
# run: /hello |
| 26 |
# run: /goodbye |