feat: 增加配置文件支持主机地址,优化配置加载和保存逻辑

This commit is contained in:
iHeyTang
2025-08-11 10:25:21 +08:00
parent 163eac55fe
commit 039e3fc0bb
3 changed files with 483 additions and 115 deletions

View File

@@ -13,24 +13,36 @@ CONFIG_FILE = os.path.join(NODE_DIR, "config.json")
# 默认配置
DEFAULT_CONFIG = {
"api_url": ""
"api_url": "",
"host": ""
}
def load_config():
"""加载配置文件,如果文件不存在则创建"""
if not os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE, 'w') as f:
json.dump(DEFAULT_CONFIG, f)
with open(CONFIG_FILE, 'w', encoding='utf-8') as f:
json.dump(DEFAULT_CONFIG, f, indent=4, ensure_ascii=False)
return DEFAULT_CONFIG
try:
with open(CONFIG_FILE, 'r', encoding='utf-8') as f:
config = json.load(f)
# 确保所有必需的字段都存在
for key in DEFAULT_CONFIG:
if key not in config:
config[key] = DEFAULT_CONFIG[key]
return config
except (json.JSONDecodeError, FileNotFoundError):
# 如果配置文件损坏或不存在,重新创建
with open(CONFIG_FILE, 'w', encoding='utf-8') as f:
json.dump(DEFAULT_CONFIG, f, indent=4, ensure_ascii=False)
return DEFAULT_CONFIG
with open(CONFIG_FILE, 'r') as f:
return json.load(f)
def save_config(config_data):
"""保存配置到文件"""
with open(CONFIG_FILE, 'w') as f:
json.dump(config_data, f, indent=4)
with open(CONFIG_FILE, 'w', encoding='utf-8') as f:
json.dump(config_data, f, indent=4, ensure_ascii=False)
# 创建一个虚拟节点类虽然它不会出现在图表中但这是ComfyUI加载自定义节点的标准方式
@@ -72,8 +84,10 @@ async def save_publisher_settings(request):
try:
data = await request.json()
api_url = data.get("api_url", "")
host = data.get("host", "")
config = load_config()
config["api_url"] = api_url
config["host"] = host
save_config(config)
return web.json_response({"status": "success", "message": "Settings saved"})
except Exception as e:
@@ -93,10 +107,19 @@ async def publish_workflow_handler(request):
config = load_config()
target_api_url = config.get("api_url")
host = config.get("host", "")
if not target_api_url:
return web.json_response({"status": "error", "message": "API URL not configured"}, status=400)
# 构建完整的URL
if host and not target_api_url.startswith(('http://', 'https://')):
# 如果提供了host且api_url不是完整URL则组合它们
full_url = host.rstrip('/') + '/' + target_api_url.lstrip('/')
else:
# 否则直接使用api_url
full_url = target_api_url
# 准备要发送到目标API的数据
payload = {
"name": workflow_name,
@@ -107,7 +130,7 @@ async def publish_workflow_handler(request):
# 使用 aiohttp 发送异步请求
async with aiohttp.ClientSession() as session:
async with session.post(target_api_url, json=payload, headers=headers) as response:
async with session.post(full_url, json=payload, headers=headers) as response:
response_text = await response.text()
if response.status == 200 or response.status == 201:
return web.json_response(
@@ -131,12 +154,21 @@ async def get_workflows_from_server(request):
try:
config = load_config()
target_api_url = config.get("api_url")
host = config.get("host", "")
if not target_api_url:
return web.json_response({"status": "error", "message": "API URL not configured"}, status=400)
# 构建完整的URL
if host and not target_api_url.startswith(('http://', 'https://')):
# 如果提供了host且api_url不是完整URL则组合它们
full_url = host.rstrip('/') + '/' + target_api_url.lstrip('/')
else:
# 否则直接使用api_url
full_url = target_api_url
async with aiohttp.ClientSession() as session:
async with session.get(target_api_url) as response:
async with session.get(full_url) as response:
if response.status != 200:
response_text = await response.text()
return web.json_response({
@@ -193,12 +225,21 @@ async def delete_workflow_version(request):
config = load_config()
target_api_url = config.get("api_url")
host = config.get("host", "")
if not target_api_url:
return web.json_response({"status": "error", "message": "API URL not configured"}, status=400)
# 构建完整的URL
if host and not target_api_url.startswith(('http://', 'https://')):
# 如果提供了host且api_url不是完整URL则组合它们
base_url = host.rstrip('/') + '/' + target_api_url.lstrip('/')
else:
# 否则直接使用api_url
base_url = target_api_url
# 构建目标URL需要对工作流名称进行URL编码
delete_url = f"{target_api_url}/{urllib.parse.quote(workflow_name)}"
delete_url = f"{base_url}/{urllib.parse.quote(workflow_name)}"
async with aiohttp.ClientSession() as session:
async with session.delete(delete_url) as response: