#!/bin/bash
set -e

# 设置环境变量
export SERVER_PORT="5000"
export AUTH_JWT_SECRET="super-secret-jwt-token-with-at-least-32-characters-long"
export AUTH_JWT_ALGORITHM="HS256"
export DATABASE_URL="postgres://supabase_admin:postgres@127.0.0.1:5432/postgres"
export DATABASE_POOL_URL="postgresql://postgres:postgres@127.0.0.1:6432/postgres"
#export DB_INSTALL_ROLES="true"
export STORAGE_BACKEND="file"
export FILE_STORAGE_BACKEND_PATH="/var/lib/storage" 
export DB_INSTALL_ROLES="false"

# 等待 gotrue 服务可用（健康检查）
echo "[storage-api] Waiting for GoTrue at /user..."
until curl -s -o /dev/null -w "%{http_code}" http://localhost:9999/user | grep -qE "^(200|401|403)$"; do
  echo "[storage-api] Still waiting for GoTrue..."
  sleep 2
done

# 启动 Node 应用
exec node /supabase/storage-api/dist/start/server.js
