ComfyUI-WorkflowPublisher/workflow_service/config.py

48 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import json
from pydantic import BaseModel, ValidationError
from pydantic_settings import BaseSettings, SettingsConfigDict
from typing import List
class ComfyUIServer(BaseModel):
"""定义单个ComfyUI服务器的配置模型。"""
http_url: str
ws_url: str
input_dir: str
output_dir: str
class Settings(BaseSettings):
"""
应用配置类,通过.env文件加载环境变量。
"""
model_config = SettingsConfigDict(env_file='.env', env_file_encoding='utf-8')
# [核心改动] 使用JSON字符串来定义一个服务器列表
COMFYUI_SERVERS_JSON: str = '[{"http_url": "http://127.0.0.1:8188", "ws_url": "ws://127.0.0.1:8188/ws", "input_dir": "/comfyui/input", "output_dir": "/comfyui/output"}]'
DB_FILE: str = "/db/workflows_service.sqlite"
# S3 和 AWS 配置保持不变
S3_BUCKET_NAME: str
AWS_ACCESS_KEY_ID: str
AWS_SECRET_ACCESS_KEY: str
AWS_REGION_NAME: str
@property
def SERVERS(self) -> List[ComfyUIServer]:
"""
解析JSON配置字符串返回一个经过验证的服务器配置对象列表。
"""
try:
server_list = json.loads(self.COMFYUI_SERVERS_JSON)
# 使用Pydantic模型进行验证和类型转换
return [ComfyUIServer(**server) for server in server_list]
except json.JSONDecodeError:
raise ValueError("COMFYUI_SERVERS_JSON 格式无效必须是有效的JSON数组。")
except ValidationError as e:
raise ValueError(f"COMFYUI_SERVERS_JSON 中的服务器配置无效: {e}")
except Exception as e:
raise e
settings = Settings()