43 lines
961 B
Python
43 lines
961 B
Python
"""Basic usage example of fastapi-route-loader."""
|
|
|
|
from fastapi import APIRouter, FastAPI
|
|
|
|
from fastapi_route_loader import RouterContainer
|
|
|
|
app = FastAPI(title="Basic Usage Example")
|
|
container = RouterContainer()
|
|
|
|
users_router = APIRouter(prefix="/users", tags=["users"])
|
|
|
|
|
|
@users_router.get("/")
|
|
def list_users() -> dict[str, list[str]]:
|
|
"""List all users."""
|
|
return {"users": ["alice", "bob"]}
|
|
|
|
|
|
@users_router.get("/{user_id}")
|
|
def get_user(user_id: int) -> dict[str, int]:
|
|
"""Get a specific user."""
|
|
return {"user_id": user_id}
|
|
|
|
|
|
posts_router = APIRouter(prefix="/posts", tags=["posts"])
|
|
|
|
|
|
@posts_router.get("/")
|
|
def list_posts() -> dict[str, list[str]]:
|
|
"""List all posts."""
|
|
return {"posts": ["post1", "post2"]}
|
|
|
|
|
|
container.add_router("users", users_router)
|
|
container.add_router("posts", posts_router)
|
|
|
|
container.register_to_app(app)
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
|
|
uvicorn.run(app, host="0.0.0.0", port=8000)
|