From c48b4bbe6c44956ca1f3da2a090b878f1171c574 Mon Sep 17 00:00:00 2001 From: Dorian Turba Date: Mon, 29 Apr 2024 02:18:38 +0200 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=94=80=20Add=20"Use=20a=20main=20rout?= =?UTF-8?q?er"=20advice?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/README.md b/README.md index 723606c..e3fb2c3 100644 --- a/README.md +++ b/README.md @@ -266,6 +266,46 @@ async def read_root(request: Request) -> dict[str, Any]: return response.json() ``` +## 7. Use a main router to group all sub-routers instead of using the `app` directly + +Instead of using the `app` directly, you can use a main router to group all sub-routers. + +```py +# project/routers/__init__.py +import fastapi + +from project.routers.account import account +from project.routers.admin import admin + +main = fastapi.APIRouter() + +main.include_router(account) +main.include_router(admin) +``` + +```py +# project/app.py +import project.routers + + +app = fastapi.FastAPI( + title="API", + description="API for project", + version="0.1.0", +) + +app.include_router(project.routers.main) + +# And many other configurations... +``` + +This way, you can keep your `app` file short and clean. The configuration of the app +will be in the `app` file, and the management of the routers will be in the +`routers` package. + +This is really useful when you have feature flag logic that include a router or not. +This code won't bloat the `app` file and will be easier to maintain. + [uvicorn]: https://www.uvicorn.org/ [run_sync]: https://anyio.readthedocs.io/en/stable/threads.html#running-a-function-in-a-worker-thread [run_in_threadpool]: https://github.com/encode/starlette/blob/9f16bf5c25e126200701f6e04330864f4a91a898/starlette/concurrency.py#L36-L42 From c9b49134b64bb1a3fd6468c10906a0a45d3fbabc Mon Sep 17 00:00:00 2001 From: Dorian Turba Date: Mon, 2 Dec 2024 08:50:33 +0000 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Marcelo Trylesinski --- README.md | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index e3fb2c3..404928a 100644 --- a/README.md +++ b/README.md @@ -271,40 +271,38 @@ async def read_root(request: Request) -> dict[str, Any]: Instead of using the `app` directly, you can use a main router to group all sub-routers. ```py -# project/routers/__init__.py -import fastapi +# project/api/__init__.py +from fastapi import APIRouter from project.routers.account import account from project.routers.admin import admin -main = fastapi.APIRouter() +main = APIRouter(prefix="/api") main.include_router(account) main.include_router(admin) ``` ```py -# project/app.py -import project.routers +# project/main.py +from fastapi import FastAPI + +from project.api import api_router -app = fastapi.FastAPI( - title="API", - description="API for project", - version="0.1.0", -) +app = FastAPI() -app.include_router(project.routers.main) +app.include_router(api_router) # And many other configurations... ``` -This way, you can keep your `app` file short and clean. The configuration of the app -will be in the `app` file, and the management of the routers will be in the -`routers` package. +This way, you can keep your `main.py` file short and clean. The configuration of the app +will be in the `main.py` file, and the management of the routers will be in the +`api` package. This is really useful when you have feature flag logic that include a router or not. -This code won't bloat the `app` file and will be easier to maintain. +This code won't bloat the `main.py` file and will be easier to maintain. [uvicorn]: https://www.uvicorn.org/ [run_sync]: https://anyio.readthedocs.io/en/stable/threads.html#running-a-function-in-a-worker-thread