Skip to content

Commit bd4acf8

Browse files
authored
Update the extension plugin config (#695)
1 parent b9a9b1e commit bd4acf8

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

backend/plugin/config/plugin.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description = '通常用于前端工程数据展示'
55
author = 'wu-clan'
66

77
[app]
8-
include = 'admin'
8+
extend = 'admin'
99

1010
[api.config]
1111
prefix = '/configs'

backend/plugin/dict/plugin.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description = '通常用于约束前端工程数据展示'
55
author = 'wu-clan'
66

77
[app]
8-
include = 'admin'
8+
extend = 'admin'
99

1010
[api.dict_data]
1111
prefix = '/dict-datas'

backend/plugin/notice/plugin.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description = '发布系统内部通知、公告'
55
author = 'wu-clan'
66

77
[app]
8-
include = 'admin'
8+
extend = 'admin'
99

1010
[api.notice]
1111
prefix = '/notices'

backend/plugin/tools.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def load_plugin_config(plugin: str) -> dict[str, Any]:
8787
def parse_plugin_config() -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
8888
"""解析插件配置"""
8989

90-
extra_plugins = []
90+
extend_plugins = []
9191
app_plugins = []
9292

9393
plugins = get_plugins()
@@ -114,9 +114,16 @@ def parse_plugin_config() -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
114114
raise PluginConfigError(f'插件 {plugin} 配置文件缺少必要字段: {", ".join(missing_fields)}')
115115

116116
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)
120127
else:
121128
if not data.get('app', {}).get('router'):
122129
raise PluginConfigError(f'应用级插件 {plugin} 配置文件缺少 app.router 配置')
@@ -135,10 +142,10 @@ def parse_plugin_config() -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
135142
run_await(current_redis_client.hset)(f'{settings.PLUGIN_REDIS_PREFIX}:status', mapping=plugin_status)
136143
run_await(current_redis_client.delete)(f'{settings.PLUGIN_REDIS_PREFIX}:changed')
137144

138-
return extra_plugins, app_plugins
145+
return extend_plugins, app_plugins
139146

140147

141-
def inject_extra_router(plugin: dict[str, Any]) -> None:
148+
def inject_extend_router(plugin: dict[str, Any]) -> None:
142149
"""
143150
扩展级插件路由注入
144151
@@ -177,9 +184,9 @@ def inject_extra_router(plugin: dict[str, Any]) -> None:
177184

178185
# 获取目标 app 路由
179186
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, ".")}'
183190
target_module = import_module_cached(target_module_path)
184191
target_router = getattr(target_module, 'router', None)
185192

@@ -230,12 +237,12 @@ def inject_app_router(plugin: dict[str, Any], target_router: APIRouter) -> None:
230237

231238
def build_final_router() -> APIRouter:
232239
"""构建最终路由"""
233-
extra_plugins, app_plugins = parse_plugin_config()
240+
extend_plugins, app_plugins = parse_plugin_config()
234241

235-
for plugin in extra_plugins:
236-
inject_extra_router(plugin)
242+
for plugin in extend_plugins:
243+
inject_extend_router(plugin)
237244

238-
# 主路由,必须在插件路由注入后导入
245+
# 主路由,必须在扩展级插件路由注入后,应用级插件路由注入前导入
239246
from backend.app.router import router as main_router
240247

241248
for plugin in app_plugins:

0 commit comments

Comments
 (0)