Spaces:
Paused
Paused
Upload start-space.sh with huggingface_hub
Browse files- start-space.sh +20 -15
start-space.sh
CHANGED
|
@@ -20,20 +20,25 @@ done
|
|
| 20 |
|
| 21 |
ADMIN_EMAIL="${ADMIN_EMAIL:-2691539771@qq.com}"
|
| 22 |
SERVER_PORT="${SERVER_PORT:-7860}"
|
| 23 |
-
DATA_DIR="${DATA_DIR:-/
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
if [ -z "${DATABASE_HOST:-}" ]; then
|
| 32 |
-
if [ ! -s /
|
| 33 |
-
su-exec postgres initdb -D
|
| 34 |
fi
|
| 35 |
|
| 36 |
-
su-exec postgres pg_ctl -D
|
| 37 |
createdb -h 127.0.0.1 -U postgres gatewaydb 2>/dev/null || true
|
| 38 |
|
| 39 |
DATABASE_HOST="127.0.0.1"
|
|
@@ -42,7 +47,7 @@ if [ -z "${DATABASE_HOST:-}" ]; then
|
|
| 42 |
DATABASE_PASSWORD=""
|
| 43 |
DATABASE_DBNAME="gatewaydb"
|
| 44 |
DATABASE_SSLMODE="disable"
|
| 45 |
-
echo "Using
|
| 46 |
else
|
| 47 |
DATABASE_PORT="${DATABASE_PORT:-5432}"
|
| 48 |
DATABASE_USER="${DATABASE_USER:-postgres}"
|
|
@@ -92,7 +97,7 @@ sync_balance_cache_once() {
|
|
| 92 |
-d "$DATABASE_DBNAME" \
|
| 93 |
-qAt \
|
| 94 |
-F '|' \
|
| 95 |
-
-c "select id, balance::double precision from users where deleted_at is null and status = 'active';" 2>/
|
| 96 |
|
| 97 |
if [ -z "$rows" ]; then
|
| 98 |
return 0
|
|
@@ -153,7 +158,7 @@ database:
|
|
| 153 |
conn_max_idle_time_minutes: ${DATABASE_CONN_MAX_IDLE_TIME_MINUTES:-5}
|
| 154 |
redis:
|
| 155 |
host: "${REDIS_HOST}"
|
| 156 |
-
port: ${REDIS_PORT}
|
| 157 |
password: "${REDIS_PASSWORD:-}"
|
| 158 |
db: ${REDIS_DB:-0}
|
| 159 |
enable_tls: ${REDIS_ENABLE_TLS:-false}
|
|
@@ -263,7 +268,7 @@ PY
|
|
| 263 |
-p "$DATABASE_PORT" \
|
| 264 |
-U "$DATABASE_USER" \
|
| 265 |
-d "$sync_dbname" \
|
| 266 |
-
-tAc "select to_regclass('public.users') is not null;" 2>/
|
| 267 |
table_exists="$(printf '%s' "$table_exists" | tr -d '[:space:]')"
|
| 268 |
admin_count="$(PGPASSWORD="${DATABASE_PASSWORD:-}" psql \
|
| 269 |
-h "$DATABASE_HOST" \
|
|
@@ -280,14 +285,14 @@ PY
|
|
| 280 |
-U "$DATABASE_USER" \
|
| 281 |
-d "$sync_dbname" \
|
| 282 |
-v ON_ERROR_STOP=1 \
|
| 283 |
-
-c "update users set password_hash = '${password_hash}', role = 'admin', status = 'active', updated_at = now() where email = '${ADMIN_EMAIL}'; insert into users (email, password_hash, role, balance, concurrency, status, created_at, updated_at) select '${ADMIN_EMAIL}', '${password_hash}', 'admin', 0, 5, 'active', now(), now() where not exists (select 1 from users where email = '${ADMIN_EMAIL}');" >/
|
| 284 |
|
| 285 |
stored_hash="$(PGPASSWORD="${DATABASE_PASSWORD:-}" psql \
|
| 286 |
-h "$DATABASE_HOST" \
|
| 287 |
-p "$DATABASE_PORT" \
|
| 288 |
-U "$DATABASE_USER" \
|
| 289 |
-d "$sync_dbname" \
|
| 290 |
-
-tAc "select password_hash from users where role = 'admin' limit 1;" 2>/
|
| 291 |
export STORED_ADMIN_PASSWORD_HASH="$stored_hash"
|
| 292 |
verify_result="$(python3 - <<'PY'
|
| 293 |
import bcrypt
|
|
@@ -311,7 +316,7 @@ PY
|
|
| 311 |
done
|
| 312 |
|
| 313 |
echo "Admin password sync skipped: admin user not found before timeout." >&2
|
| 314 |
-
cat /
|
| 315 |
}
|
| 316 |
|
| 317 |
maybe_skip_auto_setup
|
|
|
|
| 20 |
|
| 21 |
ADMIN_EMAIL="${ADMIN_EMAIL:-2691539771@qq.com}"
|
| 22 |
SERVER_PORT="${SERVER_PORT:-7860}"
|
| 23 |
+
DATA_DIR="${DATA_DIR:-/data/gateway-data}"
|
| 24 |
|
| 25 |
+
# Use /data for persistent storage (HF Storage Bucket mounted at /data)
|
| 26 |
+
PG_DATA="/data/gateway-postgres"
|
| 27 |
+
REDIS_DATA="/data/gateway-redis"
|
| 28 |
|
| 29 |
+
mkdir -p "$DATA_DIR" "$REDIS_DATA" "$PG_DATA"
|
| 30 |
+
chown -R sub2api:sub2api "$DATA_DIR" "$REDIS_DATA"
|
| 31 |
+
chown -R postgres:postgres "$PG_DATA"
|
| 32 |
+
|
| 33 |
+
# Start Redis with persistence (appendonly for durability across restarts)
|
| 34 |
+
redis-server --daemonize yes --bind 127.0.0.1 --port 6379 --dir "$REDIS_DATA" --appendonly yes --appendfilename "appendonly.aof" --save "60 1 300 10" --dbfilename "dump.rdb"
|
| 35 |
|
| 36 |
if [ -z "${DATABASE_HOST:-}" ]; then
|
| 37 |
+
if [ ! -s "$PG_DATA/PG_VERSION" ]; then
|
| 38 |
+
su-exec postgres initdb -D "$PG_DATA" --auth=trust >"$PG_DATA/init.log"
|
| 39 |
fi
|
| 40 |
|
| 41 |
+
su-exec postgres pg_ctl -D "$PG_DATA" -l "$PG_DATA/gateway-postgres.log" -o "-c listen_addresses=127.0.0.1 -c port=5432 -c unix_socket_directories=/tmp" start
|
| 42 |
createdb -h 127.0.0.1 -U postgres gatewaydb 2>/dev/null || true
|
| 43 |
|
| 44 |
DATABASE_HOST="127.0.0.1"
|
|
|
|
| 47 |
DATABASE_PASSWORD=""
|
| 48 |
DATABASE_DBNAME="gatewaydb"
|
| 49 |
DATABASE_SSLMODE="disable"
|
| 50 |
+
echo "Using persistent local PostgreSQL (data at $PG_DATA). Data survives Space restarts."
|
| 51 |
else
|
| 52 |
DATABASE_PORT="${DATABASE_PORT:-5432}"
|
| 53 |
DATABASE_USER="${DATABASE_USER:-postgres}"
|
|
|
|
| 97 |
-d "$DATABASE_DBNAME" \
|
| 98 |
-qAt \
|
| 99 |
-F '|' \
|
| 100 |
+
-c "select id, balance::double precision from users where deleted_at is null and status = 'active';" 2>/data/balance-cache-sync.err || true)"
|
| 101 |
|
| 102 |
if [ -z "$rows" ]; then
|
| 103 |
return 0
|
|
|
|
| 158 |
conn_max_idle_time_minutes: ${DATABASE_CONN_MAX_IDLE_TIME_MINUTES:-5}
|
| 159 |
redis:
|
| 160 |
host: "${REDIS_HOST}"
|
| 161 |
+
port: "${REDIS_PORT}"
|
| 162 |
password: "${REDIS_PASSWORD:-}"
|
| 163 |
db: ${REDIS_DB:-0}
|
| 164 |
enable_tls: ${REDIS_ENABLE_TLS:-false}
|
|
|
|
| 268 |
-p "$DATABASE_PORT" \
|
| 269 |
-U "$DATABASE_USER" \
|
| 270 |
-d "$sync_dbname" \
|
| 271 |
+
-tAc "select to_regclass('public.users') is not null;" 2>/data/admin-user-check.err || true)"
|
| 272 |
table_exists="$(printf '%s' "$table_exists" | tr -d '[:space:]')"
|
| 273 |
admin_count="$(PGPASSWORD="${DATABASE_PASSWORD:-}" psql \
|
| 274 |
-h "$DATABASE_HOST" \
|
|
|
|
| 285 |
-U "$DATABASE_USER" \
|
| 286 |
-d "$sync_dbname" \
|
| 287 |
-v ON_ERROR_STOP=1 \
|
| 288 |
+
-c "update users set password_hash = '${password_hash}', role = 'admin', status = 'active', updated_at = now() where email = '${ADMIN_EMAIL}'; insert into users (email, password_hash, role, balance, concurrency, status, created_at, updated_at) select '${ADMIN_EMAIL}', '${password_hash}', 'admin', 0, 5, 'active', now(), now() where not exists (select 1 from users where email = '${ADMIN_EMAIL}');" >/data/admin-password-sync.out 2>/data/admin-password-sync.err
|
| 289 |
|
| 290 |
stored_hash="$(PGPASSWORD="${DATABASE_PASSWORD:-}" psql \
|
| 291 |
-h "$DATABASE_HOST" \
|
| 292 |
-p "$DATABASE_PORT" \
|
| 293 |
-U "$DATABASE_USER" \
|
| 294 |
-d "$sync_dbname" \
|
| 295 |
+
-tAc "select password_hash from users where role = 'admin' limit 1;" 2>/data/admin-password-verify.err || true)"
|
| 296 |
export STORED_ADMIN_PASSWORD_HASH="$stored_hash"
|
| 297 |
verify_result="$(python3 - <<'PY'
|
| 298 |
import bcrypt
|
|
|
|
| 316 |
done
|
| 317 |
|
| 318 |
echo "Admin password sync skipped: admin user not found before timeout." >&2
|
| 319 |
+
cat /data/admin-user-check.err >&2 || true
|
| 320 |
}
|
| 321 |
|
| 322 |
maybe_skip_auto_setup
|