feat: routers client and version prefixes

This commit is contained in:
2025-10-17 21:40:54 +04:00
parent 6b1b4109c6
commit d7c7d1775f
22 changed files with 4 additions and 5 deletions

View File

@ -36,7 +36,6 @@ async def unicorn_exception_handler(request: Request, exc: ObjectNotFoundExcepti
return JSONResponse(status_code=404, content={"detail": exc.name})
auto_include_routers(app, routers)
auto_include_routers(app, modules, True)
auto_include_routers(app, routers, True)
app.mount("/static", StaticFiles(directory="static"), name="static")

View File

View File

@ -5,6 +5,8 @@ from types import ModuleType
from fastapi import FastAPI, APIRouter
names_to_ignore = ["routers", "modules"]
def auto_include_routers(app: FastAPI, package: ModuleType, full_path: bool = False):
"""
@ -16,7 +18,6 @@ def auto_include_routers(app: FastAPI, package: ModuleType, full_path: bool = Fa
"""
package_path = Path(package.__file__).parent
base_pkg_name = package.__name__.replace("_", "-")
for _, name, _ in pkgutil.walk_packages(package.__path__, package.__name__ + "."):
module = importlib.import_module(name)
@ -33,7 +34,7 @@ def auto_include_routers(app: FastAPI, package: ModuleType, full_path: bool = Fa
parts: list[str] = list(relative_path.parts)
# Remove "routers" folder(s) from prefix parts
parts = [p for p in parts if p != "routers"]
parts = [p for p in parts if p not in names_to_ignore]
# Drop extension from filename
parts[-1] = parts[-1].replace(".py", "")
@ -41,7 +42,6 @@ def auto_include_routers(app: FastAPI, package: ModuleType, full_path: bool = Fa
if full_path:
# Use full path
prefix_parts = [p.replace("_", "-") for p in parts if p != "__init__"]
prefix_parts.insert(0, base_pkg_name)
else:
# Only use last file name
prefix_parts = [parts[-1].replace("_", "-")]