feat: routers client and version prefixes
This commit is contained in:
3
main.py
3
main.py
@ -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")
|
||||
|
||||
0
routers/crm/v1/modules/__init__.py
Normal file
0
routers/crm/v1/modules/__init__.py
Normal file
0
routers/crm/v1/modules/clients/__init__.py
Normal file
0
routers/crm/v1/modules/clients/__init__.py
Normal file
0
routers/crm/v1/modules/fulfillment_base/__init__.py
Normal file
0
routers/crm/v1/modules/fulfillment_base/__init__.py
Normal 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("_", "-")]
|
||||
|
||||
Reference in New Issue
Block a user