#!/bin/sh # httpSMS unified gateway entrypoint for Hugging Face Spaces (Docker SDK, uid=1000, port 7860) # 1) Start the Go Fiber API so GORM auto-migrates the PostgreSQL schema # 2) Idempotently seed the internal CloudEvents queue system user # 3) Supervise the API (auto-restart) and serve nginx on :7860 in the foreground set -e API_BIN="${API_BIN:-/usr/local/bin/httpsms-api}" API_PORT="${APP_PORT:-8000}" API_BASE="http://127.0.0.1:${API_PORT}" # --- Configuration --------------------------------------------------- # If DATABASE_URL / REDIS_URL are not provided as Space secrets, bootstrap # ephemeral LOCAL Postgres + Redis so the Space is live immediately. # Data resets on restart; add the two secrets later to switch to a managed # database without a rebuild. if [ -z "$DATABASE_URL" ] || [ -z "$REDIS_URL" ]; then echo "[entrypoint] BOOTSTRAP MODE: DATABASE_URL/REDIS_URL unset -> starting ephemeral local Postgres + Redis (resets on restart)" mkdir -p /tmp/pgdata /tmp/redis if [ ! -f /tmp/pgdata/PG_VERSION ]; then initdb -D /tmp/pgdata -U postgres --auth=trust fi if ! pg_ctl -D /tmp/pgdata status >/dev/null 2>&1; then pg_ctl -D /tmp/pgdata -o "-p 5432 -h 127.0.0.1 -k /tmp" -w start fi createdb -h 127.0.0.1 -p 5432 -U postgres httpsms 2>/dev/null || true redis-server --port 6379 --bind 127.0.0.1 --dir /tmp/redis --save "" --daemonize yes || true export DATABASE_URL="${DATABASE_URL:-postgresql://postgres@127.0.0.1:5432/httpsms?sslmode=disable}" export DATABASE_URL_DEDICATED="${DATABASE_URL_DEDICATED:-$DATABASE_URL}" export REDIS_URL="${REDIS_URL:-redis://127.0.0.1:6379}" fi [ -n "$EVENTS_QUEUE_USER_ID" ] && [ -n "$EVENTS_QUEUE_USER_API_KEY" ] || { echo "[entrypoint] FATAL: EVENTS_QUEUE_USER_ID and EVENTS_QUEUE_USER_API_KEY must be set" >&2 exit 1 } echo "[entrypoint] phase 1: starting API on ${API_BASE} to run GORM auto-migrations" "$API_BIN" --dotenv=false & PROBE_PID=$! ready=0 i=0 while [ "$i" -lt 90 ]; do if curl -s -o /dev/null --max-time 2 "$API_BASE/"; then ready=1 break fi if ! kill -0 "$PROBE_PID" 2>/dev/null; then echo "[entrypoint] FATAL: API exited before becoming ready (check DATABASE_URL / REDIS_URL)" >&2 exit 1 fi i=$((i + 1)) sleep 2 done [ "$ready" -eq 1 ] || { echo "[entrypoint] FATAL: API not ready after 180s" >&2; exit 1; } echo "[entrypoint] API is listening (schema migrated)" echo "[entrypoint] phase 2: seeding internal CloudEvents queue system user (idempotent)" psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -q -c \ "INSERT INTO users (id, api_key, email, created_at, updated_at) VALUES ('${EVENTS_QUEUE_USER_ID}', '${EVENTS_QUEUE_USER_API_KEY}', 'system@httpsms.local', NOW(), NOW()) ON CONFLICT (id) DO UPDATE SET api_key = EXCLUDED.api_key;" echo "[entrypoint] seed OK" # Stop the migration probe instance; the supervisor below owns the API from here on kill "$PROBE_PID" 2>/dev/null || true wait "$PROBE_PID" 2>/dev/null || true echo "[entrypoint] phase 3: nginx config check + API supervisor loop" nginx -t ( while true; do "$API_BIN" --dotenv=false 2>&1 & SUPERVISED_PID=$! wait "$SUPERVISED_PID" code=$? echo "[entrypoint] API exited (code ${code}); restarting in 5s" >&2 sleep 5 done ) & SUPERVISOR_PID=$! trap 'kill "$SUPERVISOR_PID" 2>/dev/null || true; exit 0' TERM INT echo "[entrypoint] serving https on :7860 (nginx -> API :${API_PORT})" exec nginx -g 'daemon off;'