refactor: switch to full auto-discovery for module API routes
- Enhanced route discovery system with ROUTE_CONFIG support for custom
prefix, tags, and priority
- Added get_admin_api_routes() and get_vendor_api_routes() helpers that
return routes sorted by priority
- Added fallback discovery for routes/{frontend}.py when routes/api/
doesn't exist
- Updated CMS module with ROUTE_CONFIG (prefix: /content-pages,
priority: 100) to register last for catch-all routes
- Moved customers routes from routes/ to routes/api/ directory
- Updated orders module to aggregate exception routers into main routers
- Removed manual module router imports from admin and vendor API init
files, replaced with auto-discovery loop
Modules now auto-discovered: billing, inventory, orders, marketplace,
cms, customers, analytics, loyalty, messaging, monitoring, dev-tools
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -25,22 +25,18 @@ Routes can be module-gated using require_module_access() dependency.
|
||||
For multi-tenant apps, module enablement is checked at request time
|
||||
based on platform context (not at route registration time).
|
||||
|
||||
Extracted modules (app/modules/{module}/routes/):
|
||||
Self-contained modules (auto-discovered from app/modules/{module}/routes/api/admin.py):
|
||||
- billing: Subscription tiers, vendor billing, invoices
|
||||
- inventory: Stock management, inventory tracking
|
||||
- orders: Order management, fulfillment, exceptions
|
||||
- marketplace: Letzshop integration, product sync
|
||||
|
||||
Module extraction pattern:
|
||||
1. Create app/modules/{module}/ directory
|
||||
2. Create routes/admin.py with require_module_access("{module}") dependency
|
||||
3. Import module router here and include it
|
||||
4. Comment out legacy router include
|
||||
- cms: Content pages management
|
||||
- customers: Customer management
|
||||
"""
|
||||
|
||||
from fastapi import APIRouter
|
||||
|
||||
# Import all admin routers
|
||||
# Import all admin routers (legacy routes that haven't been migrated to modules)
|
||||
from . import (
|
||||
admin_users,
|
||||
audit,
|
||||
@@ -48,16 +44,11 @@ from . import (
|
||||
background_tasks,
|
||||
code_quality,
|
||||
companies,
|
||||
# content_pages - moved to app.modules.cms.routes.api.admin
|
||||
# customers - moved to app.modules.customers.routes.admin
|
||||
dashboard,
|
||||
email_templates,
|
||||
features,
|
||||
images,
|
||||
inventory,
|
||||
letzshop,
|
||||
logs,
|
||||
marketplace,
|
||||
media,
|
||||
menu_config,
|
||||
messages,
|
||||
@@ -65,8 +56,6 @@ from . import (
|
||||
modules,
|
||||
monitoring,
|
||||
notifications,
|
||||
order_item_exceptions,
|
||||
orders,
|
||||
platform_health,
|
||||
platforms,
|
||||
products,
|
||||
@@ -80,21 +69,6 @@ from . import (
|
||||
vendors,
|
||||
)
|
||||
|
||||
# Import extracted module routers
|
||||
# NOTE: Import directly from admin.py files to avoid circular imports through __init__.py
|
||||
from app.modules.billing.routes.api.admin import admin_router as billing_admin_router
|
||||
from app.modules.inventory.routes.admin import admin_router as inventory_admin_router
|
||||
from app.modules.orders.routes.admin import admin_router as orders_admin_router
|
||||
from app.modules.orders.routes.admin import admin_exceptions_router as orders_exceptions_router
|
||||
from app.modules.marketplace.routes.api.admin import admin_router as marketplace_admin_router
|
||||
from app.modules.marketplace.routes.api.admin import admin_letzshop_router as letzshop_admin_router
|
||||
|
||||
# CMS module router
|
||||
from app.modules.cms.routes.api.admin import router as cms_admin_router
|
||||
|
||||
# Customers module router
|
||||
from app.modules.customers.routes.admin import admin_router as customers_admin_router
|
||||
|
||||
# Create admin router
|
||||
router = APIRouter()
|
||||
|
||||
@@ -123,12 +97,6 @@ router.include_router(vendor_domains.router, tags=["admin-vendor-domains"])
|
||||
# Include vendor themes management endpoints
|
||||
router.include_router(vendor_themes.router, tags=["admin-vendor-themes"])
|
||||
|
||||
# Include CMS module router (self-contained module)
|
||||
router.include_router(
|
||||
cms_admin_router, prefix="/content-pages", tags=["admin-content-pages"]
|
||||
)
|
||||
# Legacy: content_pages.router moved to app.modules.cms.routes.api.admin
|
||||
|
||||
# Include platforms management endpoints (multi-platform CMS)
|
||||
router.include_router(platforms.router, tags=["admin-platforms"])
|
||||
|
||||
@@ -152,10 +120,6 @@ router.include_router(users.router, tags=["admin-users"])
|
||||
# Include admin user management endpoints (super admin only)
|
||||
router.include_router(admin_users.router, tags=["admin-admin-users"])
|
||||
|
||||
# Include customers module router (with module access control)
|
||||
router.include_router(customers_admin_router, tags=["admin-customers"])
|
||||
# Legacy: router.include_router(customers.router, tags=["admin-customers"])
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Dashboard & Statistics
|
||||
@@ -166,7 +130,7 @@ router.include_router(dashboard.router, tags=["admin-dashboard"])
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Vendor Operations (Product Catalog, Inventory & Orders) - Module-gated
|
||||
# Vendor Operations (Product Catalog)
|
||||
# ============================================================================
|
||||
|
||||
# Include marketplace product catalog management endpoints
|
||||
@@ -175,27 +139,6 @@ router.include_router(products.router, tags=["admin-marketplace-products"])
|
||||
# Include vendor product catalog management endpoints
|
||||
router.include_router(vendor_products.router, tags=["admin-vendor-products"])
|
||||
|
||||
# Include inventory module router (with module access control)
|
||||
router.include_router(inventory_admin_router, tags=["admin-inventory"])
|
||||
# Legacy: router.include_router(inventory.router, tags=["admin-inventory"])
|
||||
|
||||
# Include orders module router (with module access control)
|
||||
router.include_router(orders_admin_router, tags=["admin-orders"])
|
||||
router.include_router(orders_exceptions_router, tags=["admin-order-exceptions"])
|
||||
# Legacy: router.include_router(orders.router, tags=["admin-orders"])
|
||||
# Legacy: router.include_router(order_item_exceptions.router, tags=["admin-order-exceptions"])
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Marketplace & Imports (Module-gated)
|
||||
# ============================================================================
|
||||
|
||||
# Include marketplace module router (with module access control)
|
||||
router.include_router(marketplace_admin_router, tags=["admin-marketplace"])
|
||||
router.include_router(letzshop_admin_router, tags=["admin-letzshop"])
|
||||
# Legacy: router.include_router(marketplace.router, tags=["admin-marketplace"])
|
||||
# Legacy: router.include_router(letzshop.router, tags=["admin-letzshop"])
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Platform Administration
|
||||
@@ -236,21 +179,6 @@ router.include_router(
|
||||
)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Billing & Subscriptions (Module-gated)
|
||||
# ============================================================================
|
||||
|
||||
# Include billing module router (with module access control)
|
||||
# This router checks if the 'billing' module is enabled for the platform
|
||||
router.include_router(billing_admin_router, tags=["admin-billing"])
|
||||
|
||||
# Legacy subscriptions router (to be removed once billing module is fully tested)
|
||||
# router.include_router(subscriptions.router, tags=["admin-subscriptions"])
|
||||
|
||||
# Include feature management endpoints
|
||||
router.include_router(features.router, tags=["admin-features"])
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Code Quality & Architecture
|
||||
# ============================================================================
|
||||
@@ -263,5 +191,31 @@ router.include_router(
|
||||
# Include test runner endpoints
|
||||
router.include_router(tests.router, prefix="/tests", tags=["admin-tests"])
|
||||
|
||||
# Include feature management endpoints
|
||||
router.include_router(features.router, tags=["admin-features"])
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Auto-discovered Module Routes
|
||||
# ============================================================================
|
||||
# Routes from self-contained modules are auto-discovered and registered.
|
||||
# Modules include: billing, inventory, orders, marketplace, cms, customers
|
||||
|
||||
from app.modules.routes import get_admin_api_routes
|
||||
|
||||
for route_info in get_admin_api_routes():
|
||||
# Only pass prefix if custom_prefix is set (router already has internal prefix)
|
||||
if route_info.custom_prefix:
|
||||
router.include_router(
|
||||
route_info.router,
|
||||
prefix=route_info.custom_prefix,
|
||||
tags=route_info.tags,
|
||||
)
|
||||
else:
|
||||
router.include_router(
|
||||
route_info.router,
|
||||
tags=route_info.tags,
|
||||
)
|
||||
|
||||
# Export the router
|
||||
__all__ = ["router"]
|
||||
|
||||
90
app/api/v1/vendor/__init__.py
vendored
90
app/api/v1/vendor/__init__.py
vendored
@@ -14,43 +14,32 @@ Routes can be module-gated using require_module_access() dependency.
|
||||
For multi-tenant apps, module enablement is checked at request time
|
||||
based on platform context (not at route registration time).
|
||||
|
||||
Extracted modules (app/modules/{module}/routes/):
|
||||
Self-contained modules (auto-discovered from app/modules/{module}/routes/api/vendor.py):
|
||||
- billing: Subscription tiers, vendor billing, invoices
|
||||
- inventory: Stock management, inventory tracking
|
||||
- orders: Order management, fulfillment, exceptions
|
||||
- marketplace: Letzshop integration, product sync
|
||||
|
||||
Module extraction pattern:
|
||||
1. Create app/modules/{module}/ directory
|
||||
2. Create routes/vendor.py with require_module_access("{module}") dependency
|
||||
3. Import module router here and include it
|
||||
4. Comment out legacy router include
|
||||
- cms: Content pages management
|
||||
- customers: Customer management
|
||||
"""
|
||||
|
||||
from fastapi import APIRouter
|
||||
|
||||
# Import all sub-routers (JSON API only)
|
||||
# Import all sub-routers (legacy routes that haven't been migrated to modules)
|
||||
from . import (
|
||||
analytics,
|
||||
auth,
|
||||
billing,
|
||||
# content_pages - moved to app.modules.cms.routes.api.vendor
|
||||
# customers - moved to app.modules.customers.routes.vendor
|
||||
dashboard,
|
||||
email_settings,
|
||||
email_templates,
|
||||
features,
|
||||
info,
|
||||
inventory,
|
||||
invoices,
|
||||
letzshop,
|
||||
marketplace,
|
||||
media,
|
||||
messages,
|
||||
notifications,
|
||||
onboarding,
|
||||
order_item_exceptions,
|
||||
orders,
|
||||
payments,
|
||||
products,
|
||||
profile,
|
||||
@@ -59,21 +48,6 @@ from . import (
|
||||
usage,
|
||||
)
|
||||
|
||||
# Import extracted module routers
|
||||
# NOTE: Import directly from vendor.py files to avoid circular imports through __init__.py
|
||||
from app.modules.billing.routes.api.vendor import vendor_router as billing_vendor_router
|
||||
from app.modules.inventory.routes.vendor import vendor_router as inventory_vendor_router
|
||||
from app.modules.orders.routes.vendor import vendor_router as orders_vendor_router
|
||||
from app.modules.orders.routes.vendor import vendor_exceptions_router as orders_exceptions_router
|
||||
from app.modules.marketplace.routes.api.vendor import vendor_router as marketplace_vendor_router
|
||||
from app.modules.marketplace.routes.api.vendor import vendor_letzshop_router as letzshop_vendor_router
|
||||
|
||||
# CMS module router
|
||||
from app.modules.cms.routes.api.vendor import router as cms_vendor_router
|
||||
|
||||
# Customers module router
|
||||
from app.modules.customers.routes.vendor import vendor_router as customers_vendor_router
|
||||
|
||||
# Create vendor router
|
||||
router = APIRouter()
|
||||
|
||||
@@ -97,50 +71,44 @@ router.include_router(email_templates.router, tags=["vendor-email-templates"])
|
||||
router.include_router(email_settings.router, tags=["vendor-email-settings"])
|
||||
router.include_router(onboarding.router, tags=["vendor-onboarding"])
|
||||
|
||||
# Business operations (with prefixes: /products/*, /orders/*, etc.)
|
||||
# Business operations (with prefixes: /products/*, etc.)
|
||||
router.include_router(products.router, tags=["vendor-products"])
|
||||
|
||||
# Include orders module router (with module access control)
|
||||
router.include_router(orders_vendor_router, tags=["vendor-orders"])
|
||||
router.include_router(orders_exceptions_router, tags=["vendor-order-exceptions"])
|
||||
# Legacy: router.include_router(orders.router, tags=["vendor-orders"])
|
||||
# Legacy: router.include_router(order_item_exceptions.router, tags=["vendor-order-exceptions"])
|
||||
|
||||
router.include_router(invoices.router, tags=["vendor-invoices"])
|
||||
|
||||
# Include customers module router (with module access control)
|
||||
router.include_router(customers_vendor_router, tags=["vendor-customers"])
|
||||
# Legacy: router.include_router(customers.router, tags=["vendor-customers"])
|
||||
|
||||
router.include_router(team.router, tags=["vendor-team"])
|
||||
|
||||
# Include inventory module router (with module access control)
|
||||
router.include_router(inventory_vendor_router, tags=["vendor-inventory"])
|
||||
# Legacy: router.include_router(inventory.router, tags=["vendor-inventory"])
|
||||
|
||||
# Include marketplace module router (with module access control)
|
||||
router.include_router(marketplace_vendor_router, tags=["vendor-marketplace"])
|
||||
router.include_router(letzshop_vendor_router, tags=["vendor-letzshop"])
|
||||
# Legacy: router.include_router(marketplace.router, tags=["vendor-marketplace"])
|
||||
# Legacy: router.include_router(letzshop.router, tags=["vendor-letzshop"])
|
||||
|
||||
# Services (with prefixes: /payments/*, /media/*, etc.)
|
||||
router.include_router(payments.router, tags=["vendor-payments"])
|
||||
router.include_router(media.router, tags=["vendor-media"])
|
||||
router.include_router(notifications.router, tags=["vendor-notifications"])
|
||||
router.include_router(messages.router, tags=["vendor-messages"])
|
||||
router.include_router(analytics.router, tags=["vendor-analytics"])
|
||||
|
||||
# Include billing module router (with module access control)
|
||||
router.include_router(billing_vendor_router, tags=["vendor-billing"])
|
||||
# Legacy: router.include_router(billing.router, tags=["vendor-billing"])
|
||||
|
||||
router.include_router(features.router, tags=["vendor-features"])
|
||||
router.include_router(usage.router, tags=["vendor-usage"])
|
||||
|
||||
# CMS module router (self-contained module)
|
||||
router.include_router(cms_vendor_router, tags=["vendor-content-pages"])
|
||||
# Legacy: content_pages.router moved to app.modules.cms.routes.api.vendor
|
||||
|
||||
# ============================================================================
|
||||
# Auto-discovered Module Routes
|
||||
# ============================================================================
|
||||
# Routes from self-contained modules are auto-discovered and registered.
|
||||
# Modules include: billing, inventory, orders, marketplace, cms, customers
|
||||
# Routes are sorted by priority, so catch-all routes (CMS) come last.
|
||||
|
||||
from app.modules.routes import get_vendor_api_routes
|
||||
|
||||
for route_info in get_vendor_api_routes():
|
||||
# Only pass prefix if custom_prefix is set (router already has internal prefix)
|
||||
if route_info.custom_prefix:
|
||||
router.include_router(
|
||||
route_info.router,
|
||||
prefix=route_info.custom_prefix,
|
||||
tags=route_info.tags,
|
||||
)
|
||||
else:
|
||||
router.include_router(
|
||||
route_info.router,
|
||||
tags=route_info.tags,
|
||||
)
|
||||
|
||||
|
||||
# Vendor info endpoint - MUST BE LAST! Has catch-all GET /{vendor_code}
|
||||
router.include_router(info.router, tags=["vendor-info"])
|
||||
|
||||
Reference in New Issue
Block a user