Files
yuming 2c1c1b6a2f
部署到群晖 / deploy (push) Successful in 41s
fix: 已配置代理时不被环境变量覆盖
2026-04-25 22:44:24 +08:00

55 lines
2.3 KiB
Bash

#!/bin/sh
# Docker 容器入口脚本
# 确保持久化目录和符号链接就绪,然后启动应用
# API 凭证通过 Web 配置向导填写,无需手动配置
set -e
APPDATA="/app/appdata"
# 确保持久化子目录存在
mkdir -p "$APPDATA/sessions" "$APPDATA/temp" "$APPDATA/log"
# 确保 data.yaml 文件存在
[ -f "$APPDATA/data.yaml" ] || touch "$APPDATA/data.yaml"
# 创建符号链接,让应用从 /app/ 工作目录找到配置和数据
# config.yaml 可能不存在——应用启动时会自动创建默认版本
[ -L /app/config.yaml ] || ln -sf "$APPDATA/config.yaml" /app/config.yaml
[ -L /app/data.yaml ] || ln -sf "$APPDATA/data.yaml" /app/data.yaml
[ -L /app/sessions ] || ln -sf "$APPDATA/sessions" /app/sessions
[ -L /app/temp ] || ln -sf "$APPDATA/temp" /app/temp
[ -L /app/log ] || ln -sf "$APPDATA/log" /app/log
# 若 config.yaml 已存在且设置了 WEB_PORT 环境变量,更新配置文件里的端口
if [ -n "$WEB_PORT" ] && [ -f "$APPDATA/config.yaml" ]; then
sed -i "s/^web_port:.*/web_port: ${WEB_PORT}/" "$APPDATA/config.yaml"
fi
# 仅在首次启动(config.yaml 中尚未配置 proxy)时,使用环境变量初始化代理
# 已经在 Web 界面保存过的代理配置不会被环境变量覆盖
if [ -n "$PROXY_HOSTNAME" ] && [ -f "$APPDATA/config.yaml" ]; then
python3 -c "
import yaml, os, sys
path = '$APPDATA/config.yaml'
with open(path) as f:
cfg = yaml.safe_load(f) or {}
# 仅当 proxy 不存在或 hostname 为空时才用环境变量初始化
existing = cfg.get('proxy') or {}
if not existing.get('hostname'):
cfg['proxy'] = {
'scheme': os.environ.get('PROXY_SCHEME', 'socks5'),
'hostname': os.environ.get('PROXY_HOSTNAME'),
'port': int(os.environ.get('PROXY_PORT', 7890))
}
with open(path, 'w') as f:
yaml.dump(cfg, f, allow_unicode=True, default_flow_style=False)
print('[entrypoint] 首次启动,代理配置已写入: ' + cfg['proxy']['scheme'] + '://' + cfg['proxy']['hostname'] + ':' + str(cfg['proxy']['port']))
else:
print('[entrypoint] 检测到已有代理配置,跳过环境变量覆盖: ' + existing.get('scheme', '') + '://' + existing.get('hostname', '') + ':' + str(existing.get('port', '')))
"
fi
echo "[entrypoint] 启动应用..."
exec python3 /app/media_downloader.py