@@ -87,7 +87,7 @@ def load_plugin_config(plugin: str) -> dict[str, Any]:
87
87
def parse_plugin_config () -> tuple [list [dict [str , Any ]], list [dict [str , Any ]]]:
88
88
"""解析插件配置"""
89
89
90
- extra_plugins = []
90
+ extend_plugins = []
91
91
app_plugins = []
92
92
93
93
plugins = get_plugins ()
@@ -114,9 +114,16 @@ def parse_plugin_config() -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
114
114
raise PluginConfigError (f'插件 { plugin } 配置文件缺少必要字段: { ", " .join (missing_fields )} ' )
115
115
116
116
if data .get ('api' ):
117
- if not data .get ('app' , {}).get ('include' ):
118
- raise PluginConfigError (f'扩展级插件 { plugin } 配置文件缺少 app.include 配置' )
119
- extra_plugins .append (data )
117
+ # TODO: 删除过时的 include 配置
118
+ include = data .get ('app' , {}).get ('include' )
119
+ if include :
120
+ warnings .warn (
121
+ f'插件 { plugin } 配置 app.include 即将在未来版本中弃用,请尽快更新配置为 app.extend, 详情:https://fastapi-practices.github.io/fastapi_best_architecture_docs/plugin/dev.html#%E6%8F%92%E4%BB%B6%E9%85%8D%E7%BD%AE' ,
122
+ FutureWarning ,
123
+ )
124
+ if not include and not data .get ('app' , {}).get ('extend' ):
125
+ raise PluginConfigError (f'扩展级插件 { plugin } 配置文件缺少 app.extend 配置' )
126
+ extend_plugins .append (data )
120
127
else :
121
128
if not data .get ('app' , {}).get ('router' ):
122
129
raise PluginConfigError (f'应用级插件 { plugin } 配置文件缺少 app.router 配置' )
@@ -135,10 +142,10 @@ def parse_plugin_config() -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
135
142
run_await (current_redis_client .hset )(f'{ settings .PLUGIN_REDIS_PREFIX } :status' , mapping = plugin_status )
136
143
run_await (current_redis_client .delete )(f'{ settings .PLUGIN_REDIS_PREFIX } :changed' )
137
144
138
- return extra_plugins , app_plugins
145
+ return extend_plugins , app_plugins
139
146
140
147
141
- def inject_extra_router (plugin : dict [str , Any ]) -> None :
148
+ def inject_extend_router (plugin : dict [str , Any ]) -> None :
142
149
"""
143
150
扩展级插件路由注入
144
151
@@ -177,9 +184,9 @@ def inject_extra_router(plugin: dict[str, Any]) -> None:
177
184
178
185
# 获取目标 app 路由
179
186
relative_path = os .path .relpath (root , plugin_api_path )
180
- target_module_path = (
181
- f'backend.app. { plugin .get (" app" , {}).get (" include" ) } .api. { relative_path . replace ( os . sep , "." ) } '
182
- )
187
+ # TODO: 删除过时的 include 配置
188
+ app_name = plugin .get (' app' , {}).get (' include' ) or plugin . get ( 'app' , {}). get ( 'extend' )
189
+ target_module_path = f'backend.app. { app_name } .api. { relative_path . replace ( os . sep , "." ) } '
183
190
target_module = import_module_cached (target_module_path )
184
191
target_router = getattr (target_module , 'router' , None )
185
192
@@ -230,12 +237,12 @@ def inject_app_router(plugin: dict[str, Any], target_router: APIRouter) -> None:
230
237
231
238
def build_final_router () -> APIRouter :
232
239
"""构建最终路由"""
233
- extra_plugins , app_plugins = parse_plugin_config ()
240
+ extend_plugins , app_plugins = parse_plugin_config ()
234
241
235
- for plugin in extra_plugins :
236
- inject_extra_router (plugin )
242
+ for plugin in extend_plugins :
243
+ inject_extend_router (plugin )
237
244
238
- # 主路由,必须在插件路由注入后导入
245
+ # 主路由,必须在扩展级插件路由注入后,应用级插件路由注入前导入
239
246
from backend .app .router import router as main_router
240
247
241
248
for plugin in app_plugins :
0 commit comments