将项目配置系统从依赖.env文件和环境变量迁移到使用Pydantic模型进行集中管理。此举通过移除`python-dotenv`库简化了环境设置,并提高了配置的类型安全性和可维护性。 主要变更包括: - 移除`bot.py`中的.env加载逻辑。 - 新增`ServerConfig`模型来管理服务器的主机和端口。 - 更新`src/common/server.py`和`src/common/message/api.py`以从全局配置对象获取服务器设置,取代了`os.environ`。 - 从配置中移除了已废弃的`MaizoneIntercomConfig`。 - 在`bot_config_template.toml`中添加了新的`[server]`配置部分。
60 lines
2.3 KiB
Python
60 lines
2.3 KiB
Python
from src.common.server import get_global_server
|
|
import os
|
|
import importlib.metadata
|
|
from maim_message import MessageServer
|
|
from src.common.logger import get_logger
|
|
from src.config.config import global_config
|
|
|
|
global_api = None
|
|
|
|
|
|
def get_global_api() -> MessageServer: # sourcery skip: extract-method
|
|
"""获取全局MessageServer实例"""
|
|
global global_api
|
|
if global_api is None:
|
|
# 检查maim_message版本
|
|
try:
|
|
maim_message_version = importlib.metadata.version("maim_message")
|
|
version_compatible = [int(x) for x in maim_message_version.split(".")] >= [0, 3, 3]
|
|
except (importlib.metadata.PackageNotFoundError, ValueError):
|
|
version_compatible = False
|
|
|
|
# 读取配置项
|
|
maim_message_config = global_config.maim_message
|
|
|
|
# 设置基本参数
|
|
kwargs = {
|
|
"host": global_config.server.host,
|
|
"port": int(global_config.server.port),
|
|
"app": get_global_server().get_app(),
|
|
}
|
|
|
|
# 只有在版本 >= 0.3.0 时才使用高级特性
|
|
if version_compatible:
|
|
# 添加自定义logger
|
|
maim_message_logger = get_logger("maim_message")
|
|
kwargs["custom_logger"] = maim_message_logger
|
|
|
|
# 添加token认证
|
|
if maim_message_config.auth_token and len(maim_message_config.auth_token) > 0:
|
|
kwargs["enable_token"] = True
|
|
|
|
if maim_message_config.use_custom:
|
|
# 添加WSS模式支持
|
|
del kwargs["app"]
|
|
kwargs["host"] = maim_message_config.host
|
|
kwargs["port"] = maim_message_config.port
|
|
kwargs["mode"] = maim_message_config.mode
|
|
if maim_message_config.use_wss:
|
|
if maim_message_config.cert_file:
|
|
kwargs["ssl_certfile"] = maim_message_config.cert_file
|
|
if maim_message_config.key_file:
|
|
kwargs["ssl_keyfile"] = maim_message_config.key_file
|
|
kwargs["enable_custom_uvicorn_logger"] = False
|
|
|
|
global_api = MessageServer(**kwargs)
|
|
if version_compatible and maim_message_config.auth_token:
|
|
for token in maim_message_config.auth_token:
|
|
global_api.add_valid_token(token)
|
|
return global_api
|