cli2api / start.sh
hequ's picture
Update start.sh
63cbd34 verified
#!/usr/bin/env sh
set -eu
# ===== 基本参数(全部走 /tmp)=====
CONFIG_PATH="${CONFIG_PATH:-/tmp/config.yaml}"
PORT="${PORT:-8317}"
AUTH_DIR="${AUTH_DIR:-/tmp/auths}"
# ===== 管理与开关 =====
REMOTE_SECRET="${REMOTE_SECRET:-${MANAGEMENT_PASSWORD:-}}"
ALLOW_REMOTE="${ALLOW_REMOTE:-true}"
DISABLE_PANEL="${DISABLE_PANEL:-false}"
DEBUG="${DEBUG:-false}"
LOG_TO_FILE="${LOG_TO_FILE:-false}"
USAGE_STATS_ENABLED="${USAGE_STATS_ENABLED:-true}" # 默认开启统计
REQUEST_RETRY="${REQUEST_RETRY:-3}"
SWITCH_PROJECT="${SWITCH_PROJECT:-true}"
SWITCH_PREVIEW_MODEL="${SWITCH_PREVIEW_MODEL:-true}"
# ===== 目录准备 =====
mkdir -p "$(dirname "$CONFIG_PATH")" "$AUTH_DIR" || true
# ===== 注入 API Keys(支持逗号或换行)=====
API_KEYS_INPUT="${API_KEYS:-}"
API_KEYS_YAML="[]"
if [ -n "$API_KEYS_INPUT" ]; then
tmp="/tmp/_keys.yaml"
: > "$tmp"
# 先按逗号拆,再清理空白/空行
echo "$API_KEYS_INPUT" | tr ',' '\n' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//' | sed '/^$/d' | \
while IFS= read -r key; do
esc=$(printf '%s' "$key" | sed 's/\\/\\\\/g; s/"/\\"/g')
printf ' - "%s"\n' "$esc" >> "$tmp"
done
if [ -s "$tmp" ]; then
API_KEYS_YAML="\n$(cat "$tmp")"
fi
fi
# ===== 写入 /tmp/config.yaml =====
cat > "$CONFIG_PATH" <<EOF
port: ${PORT}
remote-management:
allow-remote: ${ALLOW_REMOTE}
secret-key: "${REMOTE_SECRET}"
disable-control-panel: ${DISABLE_PANEL}
auth-dir: "${AUTH_DIR}"
debug: ${DEBUG}
logging-to-file: ${LOG_TO_FILE}
usage-statistics-enabled: ${USAGE_STATS_ENABLED}
request-retry: ${REQUEST_RETRY}
quota-exceeded:
switch-project: ${SWITCH_PROJECT}
switch-preview-model: ${SWITCH_PREVIEW_MODEL}
api-keys:${API_KEYS_YAML}
EOF
echo "INFO: config.yaml 已生成于 $CONFIG_PATH"
echo "INFO: auth-dir = $AUTH_DIR"
# --- Postgres 本地镜像兜底:只用 /tmp ---
if [ -n "${PGSTORE_DSN:-}" ]; then
export PGSTORE_LOCAL_PATH="${PGSTORE_LOCAL_PATH:-/tmp}"
mkdir -p "${PGSTORE_LOCAL_PATH}/pgstore" || true
fi
# ===== 启动:自动探测可执行文件;启动前切到 /tmp(解决管理静态资源缓存告警)=====
exec_with_cd() {
cd /tmp || true
exec "$1" --config "$CONFIG_PATH"
}
# 1) PATH 中尝试(大写/小写名称)
if command -v CLIProxyAPI >/dev/null 2>&1; then
exec_with_cd "$(command -v CLIProxyAPI)"
fi
if command -v cli-proxy-api >/dev/null 2>&1; then
exec_with_cd "$(command -v cli-proxy-api)"
fi
# 2) 常见绝对路径(优先你已验证可用的 /CLIProxyAPI/CLIProxyAPI)
for p in \
/CLIProxyAPI/CLIProxyAPI \
/CLIProxyAPI/cli-proxy-api \
/usr/local/bin/CLIProxyAPI \
/usr/local/bin/cli-proxy-api \
/usr/bin/CLIProxyAPI \
/usr/bin/cli-proxy-api \
/bin/CLIProxyAPI \
/bin/cli-proxy-api
do
if [ -x "$p" ]; then
echo "INFO: 使用可执行文件: $p"
exec_with_cd "$p"
fi
done
# 3) 兜底:列目录帮助定位
echo "FATAL: 找不到 CLIProxyAPI 可执行文件。列出常见目录帮助排查:" >&2
echo "------ ls -l /CLIProxyAPI ------" >&2
ls -l /CLIProxyAPI 2>/dev/null || true
echo "------ which -a CLIProxyAPI / cli-proxy-api ------" >&2
which -a CLIProxyAPI 2>/dev/null || true
which -a cli-proxy-api 2>/dev/null || true
exit 127