diff --git a/backend/core/registrar.py b/backend/core/registrar.py index f02d20215..3b3a0a94d 100644 --- a/backend/core/registrar.py +++ b/backend/core/registrar.py @@ -23,7 +23,7 @@ from backend.middleware.jwt_auth_middleware import JwtAuthMiddleware from backend.middleware.opera_log_middleware import OperaLogMiddleware from backend.middleware.state_middleware import StateMiddleware -from backend.plugin.tools import plugin_router_inject +from backend.plugin.tools import build_final_router from backend.utils.demo_site import demo_site from backend.utils.health_check import ensure_unique_route_names, http_limit_callback from backend.utils.openapi import simplify_operation_ids @@ -157,12 +157,8 @@ def register_router(app: FastAPI) -> None: """ dependencies = [Depends(demo_site)] if settings.DEMO_MODE else None - # 插件路由 - plugin_router_inject() - - # 系统路由(必须在插件路由注入后导入) - from backend.app.router import router - + # API + router = build_final_router() app.include_router(router, dependencies=dependencies) # Extra diff --git a/backend/plugin/code_generator/api/router.py b/backend/plugin/code_generator/api/router.py index bc336c1b6..6036b8319 100644 --- a/backend/plugin/code_generator/api/router.py +++ b/backend/plugin/code_generator/api/router.py @@ -3,12 +3,12 @@ from fastapi import APIRouter from backend.core.conf import settings -from backend.plugin.code_generator.api.v1.business import router as gen_business_router -from backend.plugin.code_generator.api.v1.column import router as gen_model_router +from backend.plugin.code_generator.api.v1.business import router as business_router +from backend.plugin.code_generator.api.v1.column import router as model_router from backend.plugin.code_generator.api.v1.gen import router as gen_router v1 = APIRouter(prefix=f'{settings.FASTAPI_API_V1_PATH}/gen', tags=['代码生成']) v1.include_router(gen_router) -v1.include_router(gen_business_router, prefix='/businesses') -v1.include_router(gen_model_router, prefix='/models') +v1.include_router(business_router, prefix='/businesses') +v1.include_router(model_router, prefix='/models') diff --git a/backend/plugin/tools.py b/backend/plugin/tools.py index 2a71ccf74..9ac0747ac 100644 --- a/backend/plugin/tools.py +++ b/backend/plugin/tools.py @@ -134,12 +134,13 @@ def inject_extra_router(plugin: str, data: dict[str, Any]) -> None: raise PluginInjectError(f'扩展级插件 {plugin} 路由注入失败:{str(e)}') from e -def inject_app_router(plugin: str, data: dict[str, Any]) -> None: +def inject_app_router(plugin: str, data: dict[str, Any], target_router: APIRouter) -> None: """ 应用级插件路由注入 :param plugin: 插件名称 :param data: 插件配置数据 + :param target_router: FastAPI 路由器 :return: """ module_path = f'backend.plugin.{plugin}.api.router' @@ -149,10 +150,6 @@ def inject_app_router(plugin: str, data: dict[str, Any]) -> None: if not routers or not isinstance(routers, list): raise PluginInjectError(f'应用级插件 {plugin} 配置文件存在错误,请检查') - # 获取目标路由 - target_module = import_module_cached('backend.app.router') - target_router = getattr(target_module, 'router') - for router in routers: plugin_router = getattr(module, router, None) if not plugin_router or not isinstance(plugin_router, APIRouter): @@ -166,15 +163,26 @@ def inject_app_router(plugin: str, data: dict[str, Any]) -> None: raise PluginInjectError(f'应用级插件 {plugin} 路由注入失败:{str(e)}') from e -def plugin_router_inject() -> None: - """插件路由注入""" +def build_final_router() -> APIRouter: + """构建最终路由""" + + extra_plugins = [] + app_plugins = [] + for plugin in get_plugins(): data = load_plugin_config(plugin) - # 基于插件 plugin.toml 配置文件,判断插件类型 - if data.get('api'): - inject_extra_router(plugin, data) - else: - inject_app_router(plugin, data) + (extra_plugins if data.get('api') else app_plugins).append((plugin, data)) + + for plugin, data in extra_plugins: + inject_extra_router(plugin, data) + + # 主路由,必须在插件路由注入后导入 + from backend.app.router import router as main_router + + for plugin, data in app_plugins: + inject_app_router(plugin, data, main_router) + + return main_router def _install_plugin_requirements(plugin: str, requirements_file: str) -> None: