@@ -134,12 +134,13 @@ def inject_extra_router(plugin: str, data: dict[str, Any]) -> None:
134
134
raise PluginInjectError (f'扩展级插件 { plugin } 路由注入失败:{ str (e )} ' ) from e
135
135
136
136
137
- def inject_app_router (plugin : str , data : dict [str , Any ]) -> None :
137
+ def inject_app_router (plugin : str , data : dict [str , Any ], target_router : APIRouter ) -> None :
138
138
"""
139
139
应用级插件路由注入
140
140
141
141
:param plugin: 插件名称
142
142
:param data: 插件配置数据
143
+ :param target_router: FastAPI 路由器
143
144
:return:
144
145
"""
145
146
module_path = f'backend.plugin.{ plugin } .api.router'
@@ -149,10 +150,6 @@ def inject_app_router(plugin: str, data: dict[str, Any]) -> None:
149
150
if not routers or not isinstance (routers , list ):
150
151
raise PluginInjectError (f'应用级插件 { plugin } 配置文件存在错误,请检查' )
151
152
152
- # 获取目标路由
153
- target_module = import_module_cached ('backend.app.router' )
154
- target_router = getattr (target_module , 'router' )
155
-
156
153
for router in routers :
157
154
plugin_router = getattr (module , router , None )
158
155
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:
166
163
raise PluginInjectError (f'应用级插件 { plugin } 路由注入失败:{ str (e )} ' ) from e
167
164
168
165
169
- def plugin_router_inject () -> None :
170
- """插件路由注入"""
166
+ def build_final_router () -> APIRouter :
167
+ """构建最终路由"""
168
+
169
+ extra_plugins = []
170
+ app_plugins = []
171
+
171
172
for plugin in get_plugins ():
172
173
data = load_plugin_config (plugin )
173
- # 基于插件 plugin.toml 配置文件,判断插件类型
174
- if data .get ('api' ):
175
- inject_extra_router (plugin , data )
176
- else :
177
- inject_app_router (plugin , data )
174
+ (extra_plugins if data .get ('api' ) else app_plugins ).append ((plugin , data ))
175
+
176
+ for plugin , data in extra_plugins :
177
+ inject_extra_router (plugin , data )
178
+
179
+ # 主路由,必须在插件路由注入后导入
180
+ from backend .app .router import router as main_router
181
+
182
+ for plugin , data in app_plugins :
183
+ inject_app_router (plugin , data , main_router )
184
+
185
+ return main_router
178
186
179
187
180
188
def _install_plugin_requirements (plugin : str , requirements_file : str ) -> None :
0 commit comments