(Feat): Initial Commit.
This commit is contained in:
42
examples/basic_usage.py
Normal file
42
examples/basic_usage.py
Normal file
@@ -0,0 +1,42 @@
|
||||
"""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)
|
||||
Reference in New Issue
Block a user