refactor(config): 移除.env依赖并引入服务器配置模型
将项目配置系统从依赖.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]`配置部分。
This commit is contained in:
9
bot.py
9
bot.py
@@ -7,17 +7,9 @@ import time
|
||||
import platform
|
||||
import traceback
|
||||
from pathlib import Path
|
||||
from dotenv import load_dotenv
|
||||
from rich.traceback import install
|
||||
from colorama import init, Fore
|
||||
|
||||
if os.path.exists(".env"):
|
||||
load_dotenv(".env", override=True)
|
||||
print("成功加载环境变量配置")
|
||||
else:
|
||||
print("未找到.env文件,请确保程序所需的环境变量被正确设置")
|
||||
raise FileNotFoundError(".env 文件不存在,请创建并配置所需的环境变量")
|
||||
|
||||
# maim_message imports for console input
|
||||
|
||||
# 最早期初始化日志系统,确保所有后续模块都使用正确的日志格式
|
||||
@@ -45,7 +37,6 @@ logger.info(f"已设置工作目录为: {script_dir}")
|
||||
|
||||
confirm_logger = get_logger("confirm")
|
||||
# 获取没有加载env时的环境变量
|
||||
env_mask = {key: os.getenv(key) for key in os.environ}
|
||||
|
||||
uvicorn_server = None
|
||||
driver = None
|
||||
|
||||
@@ -24,8 +24,8 @@ def get_global_api() -> MessageServer: # sourcery skip: extract-method
|
||||
|
||||
# 设置基本参数
|
||||
kwargs = {
|
||||
"host": os.environ["HOST"],
|
||||
"port": int(os.environ["PORT"]),
|
||||
"host": global_config.server.host,
|
||||
"port": int(global_config.server.port),
|
||||
"app": get_global_server().get_app(),
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ from fastapi import FastAPI, APIRouter
|
||||
from fastapi.middleware.cors import CORSMiddleware # 新增导入
|
||||
from typing import Optional
|
||||
from uvicorn import Config, Server as UvicornServer
|
||||
import os
|
||||
from src.config.config import global_config
|
||||
from rich.traceback import install
|
||||
|
||||
install(extra_lines=3)
|
||||
@@ -98,5 +98,5 @@ def get_global_server() -> Server:
|
||||
"""获取全局服务器实例"""
|
||||
global global_server
|
||||
if global_server is None:
|
||||
global_server = Server(host=os.environ["HOST"], port=int(os.environ["PORT"]))
|
||||
global_server = Server(host=global_config.server.host,port=int(global_config.server.port),)
|
||||
return global_server
|
||||
|
||||
@@ -43,8 +43,8 @@ from src.config.official_configs import (
|
||||
CrossContextConfig,
|
||||
PermissionConfig,
|
||||
CommandConfig,
|
||||
MaizoneIntercomConfig,
|
||||
PlanningSystemConfig,
|
||||
ServerConfig,
|
||||
)
|
||||
|
||||
from .api_ada_configs import (
|
||||
@@ -399,9 +399,7 @@ class Config(ValidatedConfigBase):
|
||||
cross_context: CrossContextConfig = Field(
|
||||
default_factory=lambda: CrossContextConfig(), description="跨群聊上下文共享配置"
|
||||
)
|
||||
maizone_intercom: MaizoneIntercomConfig = Field(
|
||||
default_factory=lambda: MaizoneIntercomConfig(), description="Maizone互通组配置"
|
||||
)
|
||||
server: ServerConfig = Field(default_factory=lambda: ServerConfig(), description="主服务器配置")
|
||||
|
||||
|
||||
class APIAdapterConfig(ValidatedConfigBase):
|
||||
|
||||
@@ -494,6 +494,13 @@ class ExperimentalConfig(ValidatedConfigBase):
|
||||
pfc_chatting: bool = Field(default=False, description="启用PFC聊天")
|
||||
|
||||
|
||||
class ServerConfig(ValidatedConfigBase):
|
||||
"""主服务器配置类"""
|
||||
|
||||
host: str = Field(default="127.0.0.1", description="主服务器监听地址")
|
||||
port: int = Field(default=8080, description="主服务器监听端口")
|
||||
|
||||
|
||||
class MaimMessageConfig(ValidatedConfigBase):
|
||||
"""maim_message配置类"""
|
||||
|
||||
@@ -676,15 +683,6 @@ class CrossContextConfig(ValidatedConfigBase):
|
||||
|
||||
enable: bool = Field(default=False, description="是否启用跨群聊上下文共享功能")
|
||||
groups: List[ContextGroup] = Field(default_factory=list, description="上下文共享组列表")
|
||||
|
||||
|
||||
class MaizoneIntercomConfig(ValidatedConfigBase):
|
||||
"""Maizone互通组配置"""
|
||||
|
||||
enable: bool = Field(default=False, description="是否启用Maizone互通组功能")
|
||||
groups: List[ContextGroup] = Field(default_factory=list, description="Maizone互通组列表")
|
||||
|
||||
|
||||
class CommandConfig(ValidatedConfigBase):
|
||||
"""命令系统配置类"""
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
[inner]
|
||||
version = "6.8.4"
|
||||
version = "6.8.5"
|
||||
|
||||
#----以下是给开发人员阅读的,如果你只是部署了MoFox-Bot,不需要阅读----
|
||||
#如果你想要修改配置文件,请递增version的值
|
||||
@@ -481,6 +481,9 @@ insomnia_duration_minutes = [30, 60] # 单次失眠状态的持续时间范围
|
||||
# 入睡后,经过一段延迟后触发失眠判定的延迟时间(分钟),设置为范围以增加随机性
|
||||
insomnia_trigger_delay_minutes = [15, 45]
|
||||
|
||||
[server]
|
||||
host = "127.0.0.1"
|
||||
port = 8080
|
||||
|
||||
[cross_context] # 跨群聊/私聊上下文共享配置
|
||||
# 这是总开关,用于一键启用或禁用此功能
|
||||
|
||||
Reference in New Issue
Block a user