mirror of
https://github.com/fastapi-practices/fastapi-best-architecture.git
synced 2026-09-21 21:15:13 +00:00
Add new plugin status check interface (#606)
This commit is contained in:
@@ -22,6 +22,12 @@ async def get_all_plugins() -> ResponseSchemaModel[list[dict[str, Any]]]:
|
||||
return response_base.success(data=plugins)
|
||||
|
||||
|
||||
@router.get('/new', summary='是否存在新插件', dependencies=[DependsJwtAuth])
|
||||
async def has_new_plugins() -> ResponseSchemaModel[bool]:
|
||||
plugins = await plugin_service.has_new()
|
||||
return response_base.success(data=bool(plugins))
|
||||
|
||||
|
||||
@router.post(
|
||||
'/install/zip',
|
||||
summary='安装 zip 插件',
|
||||
|
||||
@@ -39,6 +39,11 @@ class PluginService:
|
||||
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
async def has_new() -> str | None:
|
||||
"""是否存在新插件"""
|
||||
return await redis_client.get(f'{settings.PLUGIN_REDIS_PREFIX}:new')
|
||||
|
||||
@staticmethod
|
||||
async def install_zip(*, file: UploadFile) -> None:
|
||||
"""
|
||||
@@ -89,6 +94,7 @@ class PluginService:
|
||||
zf.extractall(os.path.join(PLUGIN_DIR, plugin_name), members)
|
||||
|
||||
await install_requirements_async(plugin_name)
|
||||
await redis_client.set(f'{settings.PLUGIN_REDIS_PREFIX}:new', 'ture')
|
||||
|
||||
@staticmethod
|
||||
async def install_git(*, repo_url: str):
|
||||
@@ -112,6 +118,7 @@ class PluginService:
|
||||
raise errors.ServerError(msg='插件安装失败,请稍后重试') from e
|
||||
else:
|
||||
await install_requirements_async(repo_name)
|
||||
await redis_client.set(f'{settings.PLUGIN_REDIS_PREFIX}:new', 'ture')
|
||||
|
||||
@staticmethod
|
||||
async def uninstall(*, plugin: str):
|
||||
@@ -127,6 +134,7 @@ class PluginService:
|
||||
await uninstall_requirements_async(plugin)
|
||||
bacup_dir = os.path.join(PLUGIN_DIR, f'{plugin}.{timezone.now().strftime("%Y%m%d%H%M%S")}.backup')
|
||||
shutil.move(plugin_dir, bacup_dir)
|
||||
await redis_client.delete(f'{settings.PLUGIN_REDIS_PREFIX}:info:{plugin}')
|
||||
|
||||
@staticmethod
|
||||
async def update_status(*, plugin: str):
|
||||
@@ -140,12 +148,14 @@ class PluginService:
|
||||
if not plugin_info:
|
||||
raise errors.ForbiddenError(msg='插件不存在')
|
||||
plugin_info = json.loads(plugin_info)
|
||||
|
||||
# 更新持久缓存状态
|
||||
new_status = (
|
||||
StatusType.enable.value
|
||||
if plugin_info.get('plugin', {}).get('enable') == StatusType.disable.value
|
||||
else StatusType.disable.value
|
||||
)
|
||||
plugin_info['plugin']['enable'] = new_status
|
||||
plugin_info['plugin']['enable'] = str(new_status)
|
||||
await redis_client.set(
|
||||
f'{settings.PLUGIN_REDIS_PREFIX}:info:{plugin}', json.dumps(plugin_info, ensure_ascii=False)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user