Skip to content

Commit ae0c9c4

Browse files
committed
Added automatic route loading
1 parent bb0ce7a commit ae0c9c4

2 files changed

Lines changed: 16 additions & 11 deletions

File tree

backend/app/constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from pathlib import Path
2+
3+
APP_PATH = Path(__file__).parent

backend/app/main.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1+
from importlib import import_module
2+
13
import sentry_sdk
24
from fastapi import APIRouter, FastAPI
35
from fastapi.routing import APIRoute
46
from starlette.middleware.cors import CORSMiddleware
57

6-
from app.auth import router as auth_router
78
from app.config import settings
8-
from app.items import router as items_router
9-
from app.private import router as private_router
10-
from app.users import router as users_router
11-
from app.utils import router as utils_router
9+
from app.constants import APP_PATH
1210

1311

1412
def custom_generate_unique_id(route: APIRoute) -> str:
@@ -34,13 +32,17 @@ def custom_generate_unique_id(route: APIRoute) -> str:
3432
allow_headers=["*"],
3533
)
3634

35+
3736
api_router = APIRouter()
38-
api_router.include_router(auth_router.router)
39-
api_router.include_router(users_router.router)
40-
api_router.include_router(utils_router.router)
41-
api_router.include_router(items_router.router)
4237

43-
if settings.ENVIRONMENT == "local":
44-
api_router.include_router(private_router.router)
38+
for model_files in APP_PATH.glob("*/router.py"):
39+
module_name = model_files.parent.name
40+
router = import_module(f"app.{module_name}.router").router
41+
42+
if module_name == "private":
43+
if settings.ENVIRONMENT == "local":
44+
api_router.include_router(router)
45+
else:
46+
api_router.include_router(router)
4547

4648
app.include_router(api_router, prefix=settings.API_V1_STR)

0 commit comments

Comments
 (0)