File size: 3,209 Bytes
1d7e57f
 
36d870e
1d7e57f
a007882
8265395
1d7e57f
8265395
1d7e57f
a007882
1d7e57f
a007882
 
1d7e57f
 
36d870e
1d7e57f
 
 
 
a007882
8265395
 
63cbd34
1d7e57f
 
 
 
 
63cbd34
1d7e57f
 
 
 
 
 
 
 
 
 
a007882
1d7e57f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36d870e
 
 
63cbd34
 
 
 
 
 
 
 
 
 
8265395
 
a007882
 
63cbd34
a007882
 
63cbd34
a007882
 
63cbd34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a007882
 
63cbd34
 
a007882
63cbd34
a007882
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/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