Chapter 19: Creating a Website with Controller-Style Routers
MVC controllers group related actions. In FastAPI, APIRouter modules fill that role.
# web/account.py
router = APIRouter(prefix="/account", tags=["account"])
@router.get("/login")
def login_page(request: Request):
...
@router.post("/login")
def login_submit(request: Request):
...
@router.post("/logout")
def logout():
...
Controller-style organization:
api/recipes.pyfor API recipes.web/recipes.pyfor browser recipes.services/recipes.pyfor use cases.data/models.pyanddata/repositories.pyfor persistence.domain/recipes.pyfor domain rules that should not know HTTP exists.
Avoid placing all logic in route functions. Route functions should translate HTTP into application calls and translate results back into HTTP.