File size: 1,843 Bytes
1e84411
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f17bf8d
 
1e84411
 
 
 
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
#!/usr/bin/env bash
# anuma2api Hugging Face Space 启动脚本
# - 持久化目录 /data(Space Storage):config.toml + account/ + logs/
# - 首启把镜像内的 config.toml / account/ 拷进 /data
# - $PORT(HF 强制)覆盖 config 里的端口,启动 uvicorn
set -euo pipefail

DATA_DIR="${HF_DATA_DIR:-/data}"
mkdir -p "$DATA_DIR/account" "$DATA_DIR/logs"

# 首启:从镜像拷贝种子配置/账号(已存在的文件不覆盖,保留用户修改)
if [ ! -f "$DATA_DIR/config.toml" ] && [ -f "/app/config.toml" ]; then
  cp /app/config.toml "$DATA_DIR/config.toml"
fi
if [ ! -f "$DATA_DIR/account/.keep" ]; then
  cp -rn /app/account/. "$DATA_DIR/account/" 2>/dev/null || true
  touch "$DATA_DIR/account/.keep"
fi

# config.toml 的日志/账号目录指向持久目录(若未配置)
if [ -f "$DATA_DIR/config.toml" ] && ! grep -q 'dir = "/data' "$DATA_DIR/config.toml" 2>/dev/null; then
  # 在 [logging] 段补 dir = "/data/logs",在 [registry] 段补 account_dir = "/data/account"
  sed -i 's|^dir = "logs".*|dir = "/data/logs" # HF 持久化|' "$DATA_DIR/config.toml" || true
  sed -i 's|^account_dir = "account".*|account_dir = "/data/account" # HF 持久化|' "$DATA_DIR/config.toml" || true
  # 若上面 sed 没命中(键名不同),追加默认段
  grep -q 'dir = "/data/logs"' "$DATA_DIR/config.toml" || \
    printf '\n[logging]\nenabled = true\ndir = "/data/logs"\nfilename = "gateway.log"\n' >> "$DATA_DIR/config.toml"
fi

# HF 强制端口 $PORT(如 7860),缺省回退 7860(HF Space 约定端口;本地未注入 $PORT 时也监听 7860)
PORT="${PORT:-7860}"
export TWOAPI_CONFIG="$DATA_DIR/config.toml"

echo "[init] using config=$TWOAPI_CONFIG port=$PORT accounts=$(ls "$DATA_DIR/account"/*.json 2>/dev/null | wc -l)"
exec uv run uvicorn app.main:app --host 0.0.0.0 --port "$PORT"