#!/bin/bash
set -euo pipefail

echo "[gotrue] Waiting for PostgREST on localhost:3000..."

# 等待 postgrest 启动就绪
until curl -s http://localhost:3000/ >/dev/null 2>&1; do
  echo "[gotrue] Waiting..."
  sleep 1
done

echo "[gotrue] PostgREST is ready. Starting GoTrue..."

# 必要环境变量（监听默认 127.0.0.1:9999 即可）
export GOTRUE_SITE_URL="http://localhost:3000"
export GOTRUE_JWT_SECRET="super-secret-jwt-token-with-at-least-32-characters-long"
export GOTRUE_DB_DRIVER="postgres"
export DATABASE_URL="postgres://supabase_auth_admin:postgres@localhost:5432/postgres"
export API_EXTERNAL_URL="http://localhost:8000/auth/v1"
export GOTRUE_DISABLE_PHONE_MFA=true
export GOTRUE_EXTERNAL_PHONE_ENABLED=false

# 启动 GoTrue（路径视你 build 的二进制实际位置）
exec /usr/local/bin/auth
