新增了插件相关的完整领域模型、应用服务、基础设施实现,包括: 1. 插件状态、注册模式、来源等基础枚举和数据结构 2. 插件清单解析、发现、加载工具类 3. 插件注册表领域服务和内存存储实现 4. 插件相关的命令、查询、事件定义 5. 插件REST API接口和DTO映射 6. 集成了原有通道适配器到插件系统 7. 新增内置插件注册和自动发现能力
76 lines
2.4 KiB
Python
76 lines
2.4 KiB
Python
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
|
|
from yuxi.channel.application.service.auth_service import AuthService
|
|
from yuxi.channel.domain.middleware.configurable import Configurable
|
|
from yuxi.channel.domain.port.config_reload_port import ConfigReloadPort
|
|
from yuxi.channel.domain.service.pipeline import Pipeline
|
|
from yuxi.channel.infrastructure.config.channel_config import ChannelConfig
|
|
|
|
|
|
class ConfigService:
|
|
def __init__(
|
|
self,
|
|
config_data: dict,
|
|
config_reload: ConfigReloadPort,
|
|
pipeline: Pipeline,
|
|
*,
|
|
channel_config: ChannelConfig | None = None,
|
|
auth_service: AuthService | None = None,
|
|
) -> None:
|
|
self._config = config_data
|
|
self._config_reload = config_reload
|
|
self._pipeline = pipeline
|
|
self._channel_config = channel_config
|
|
self._auth_service = auth_service
|
|
self._config_hash: str | None = None
|
|
|
|
async def reload(self) -> tuple[list[str], str | None]:
|
|
reloaded = await self._config_reload.reload()
|
|
if reloaded:
|
|
self._config = reloaded
|
|
|
|
new_hash = hashlib.sha256(
|
|
json.dumps(self._config, sort_keys=True).encode(),
|
|
).hexdigest()[:8]
|
|
|
|
if new_hash == self._config_hash:
|
|
return [], new_hash
|
|
|
|
updated = self._update_configurable_middlewares()
|
|
|
|
if self._channel_config:
|
|
items = await self._channel_config.on_config_updated(self._config)
|
|
if items:
|
|
updated.extend(items)
|
|
self._sync_auth_credentials(items)
|
|
|
|
self._config_hash = new_hash
|
|
return updated, new_hash
|
|
|
|
@property
|
|
def config(self) -> dict:
|
|
return self._config
|
|
|
|
def _update_configurable_middlewares(self) -> list[str]:
|
|
updated: list[str] = []
|
|
|
|
for mw in self._pipeline.middlewares:
|
|
if isinstance(mw, Configurable):
|
|
items = mw.on_config_updated(self._config)
|
|
if items:
|
|
updated.extend(items)
|
|
|
|
return updated
|
|
|
|
def _sync_auth_credentials(self, updated_items: list[str]) -> None:
|
|
if not self._auth_service:
|
|
return
|
|
if "auth_token" in updated_items or "auth_password" in updated_items:
|
|
self._auth_service.update_credentials(
|
|
token=self._channel_config.auth_token,
|
|
password=self._channel_config.auth_password,
|
|
)
|