docs/examples/static_files/full_example.py
raw| 1 | from pathlib import Path |
| 2 | |
| 3 | from litestar import Litestar |
| 4 | from litestar.static_files import create_static_files_router |
| 5 | |
| 6 | ASSETS_DIR = Path("assets") |
| 7 | |
| 8 | |
| 9 | def on_startup(): |
| 10 | ASSETS_DIR.mkdir(exist_ok=True) |
| 11 | ASSETS_DIR.joinpath("hello.txt").write_text("Hello, world!") |
| 12 | |
| 13 | |
| 14 | app = Litestar( |
| 15 | route_handlers=[ |
| 16 | create_static_files_router(path="/static", directories=["assets"]), |
| 17 | ], |
| 18 | on_startup=[on_startup], |
| 19 | ) |
| 20 | |
| 21 | |
| 22 | # run: /static/hello.txt |