Skip to content
📦 ARCHIVED SNAPSHOT (v1.0) You are viewing historical business logic and documentation for VPS Infrastructure v1.0 (frozen on 2026-11-23, commit b3a7ad3). Switch to Latest →

2. Dual-Environment Self-Hosted Supabase Stack (PROD & DEV)

Repository: VPS Infrastructure (planovi-vps) • Rule Prefix: BR-SUPA • Documented Rules: 8 • Open [TEAM_WHY_NEEDED] Questions: 3

Business Purpose & Actors

  • Problem solved: Runs two isolated self-hosted Supabase clusters (PROD and DEV) on a single VPS, providing PostgreSQL 15 databases, PostgREST REST APIs, GoTrue authentication (email/password + Google OAuth + branded Polish auth emails), local file-backed Storage API, Deno Edge Functions runtime, Kong API gateways, and web-based Supabase Studio admin consoles.
  • Actors: Flutter app (planovi-flutter-app), PHP microservices (planovi-microservices), Converter backends (planovi-converter), Stripe webhooks, cron jobs (pg_net), and database/backend engineers using Supabase Studio.

All Features & End-to-End Flows

  1. API Gateway Routing (Kong 2.8.1, DB-less):
    • PROD (supabase-kong, proxy/kong.yml:1-38): routes /rest/v1/ -> supabase-postgrest:3000/, /auth/v1/ -> supabase-auth:9999/, /storage/v1/ -> supabase-storage:5000/, /functions/v1/ -> supabase-edge-runtime-prod:9000/ with strip_path: false.
    • DEV (supabase-kong-dev, docker-compose.yml:478): mounts ./proxy/kong-dev.yml:/var/lib/kong/kong.yml:ro (file exists on VPS host, untracked in Git).
  2. PostgreSQL 15 Databases (supabase-db & supabase-db-dev):
    • Persistent Docker named volumes supabase-db-data (PROD) and supabase-db-data-dev (DEV).
    • Network aliases db (PROD) and db-dev (DEV) on vps-network.
    • Healthcheck: pg_isready -U postgres every 5s, timeout 5s, 5 retries (docker-compose.yml:56-60,458-462).
  3. GoTrue Auth (supabase-auth & supabase-auth-dev):
    • Connects to supabase-db / supabase-db-dev with ?search_path=auth.
    • Sends transactional auth emails via smtp.resend.com:587 (user: resend, pass: ${RESEND_API_KEY}, sender name "Planovi", rate limit 200 emails/hour).
    • Fetches 6 HTML templates dynamically from http://vps-proxy:8080/email-templates/{confirm-signup,invite-user,magic-link,change-email,reset-password,reauthentication}.html with Polish subjects (docker-compose.yml:209-220,614-625).
    • Google OAuth enabled on both environments with separate credentials and callbacks:
      • PROD: ${GOOGLE_CLIENT_ID} / ${GOOGLE_CLIENT_SECRET} -> https://api.planovi.app/auth/v1/callback (docker-compose.yml:221-224).
      • DEV: ${GOOGLE_CLIENT_ID_DEV:-${GOOGLE_CLIENT_ID}} / ${GOOGLE_CLIENT_SECRET_DEV:-${GOOGLE_CLIENT_SECRET}} -> https://dev-api.planovi.app/auth/v1/callback (docker-compose.yml:626-629).
  4. Edge Functions Runtime (supabase-edge-runtime-prod & supabase-edge-runtime-dev):
    • Starts Deno main router via command: ["start", "--main-service", "/home/deno/functions/main"].
    • Mounts ./supabase/functions-prod:/home/deno/functions (PROD) and ./supabase/functions-dev:/home/deno/functions (DEV).
    • Injects integration credentials for Gemini, Dealflow, Scheduler, InCharge, Resend, Stripe, Google OAuth, and internal Kong URL (http://supabase-kong:8000 / http://supabase-kong-dev:8000).
  5. PostgREST & Storage API:
    • PostgREST exposes schemas public,storage,graphql_public,website with anon role anon (docker-compose.yml:267-268,672-673).
    • Storage API (v1.11.0) uses STORAGE_BACKEND: file mounted to supabase-storage-data / supabase-storage-data-dev (/var/lib/storage), FILE_SIZE_LIMIT: "52428800" (50 MiB), DATABASE_MIGRATIONS_RUN: "false" (docker-compose.yml:244-252,649-657).
  6. Supabase Studio & Postgres Meta:
    • Studio connects to supabase-pg-meta:8080 / supabase-pg-meta-dev:8080 and persists SQL snippets in ./supabase/snippets-prod and ./supabase/snippets-dev (docker-compose.yml:111,513).
  7. One-Time Cloud-to-VPS Storage Migration (scripts/migrate-storage-files.py):
    • Queries SELECT bucket_id, name FROM storage.objects; via docker exec -i supabase-db psql, downloads each file from legacy Supabase Cloud public endpoint (/storage/v1/object/public/{bucket}/{name}), falls back to /authenticated/ on exception, and writes directly to /var/lib/storage/{bucket}/{name}.

Hard Business Rules, Validations, Calculations & Edge Cases

  • BR-SUPA-01 (Exposed PostgREST Schemas): Both PROD and DEV PostgREST and Studio expose exactly public,storage,graphql_public,website (docker-compose.yml:106,267,508,672). Any custom schema outside this list is inaccessible via REST API.
  • BR-SUPA-02 (Storage File Size Limit & Migration Lock): Both PROD and DEV Storage APIs enforce FILE_SIZE_LIMIT = 52428800 bytes (50 MiB) and disable auto-migrations (DATABASE_MIGRATIONS_RUN = "false") (docker-compose.yml:246,250,651,655).
  • BR-SUPA-03 (GoTrue Redirect URI Allowlist & Rate Limit): Both PROD and DEV GoTrue allow redirects to panel.planovi.app, dev-panel.planovi.app, localhost:3000, and localhost:8000 (with /** wildcards) and cap outbound auth emails at GOTRUE_RATE_LIMIT_EMAIL_SENT = "200" (docker-compose.yml:195,208,600,613).
  • BR-SUPA-04 (PROD vs DEV Environment Isolation & Shared Secrets): PROD and DEV have completely separate PostgreSQL volumes (supabase-db-data vs supabase-db-data-dev), storage volumes (supabase-storage-data vs supabase-storage-data-dev), and Edge Function directories (functions-prod vs functions-dev). However, both stacks share the same POSTGRES_PASSWORD, JWT_SECRET, SUPABASE_ANON_KEY, SUPABASE_SERVICE_ROLE_KEY, RESEND_API_KEY, GEMINI_API_KEY, STRIPE_* keys, and DEALFLOW_BASE_URL/SCHEDULER_BASE_URL (docker-compose.yml:48-150,450-555).
  • BR-SUPA-05 (PROD vs DEV Override Secrets): Only SITE_URL_DEV, GOOGLE_CLIENT_ID_DEV, GOOGLE_CLIENT_SECRET_DEV, DEALFLOW_API_SECRET_DEV, SCHEDULER_API_SECRET_DEV, and SIGNING_SECRET_DEV are split for DEV with ${VAR_DEV:-${VAR}} fallback (except SIGNING_SECRET_DEV which has no PROD counterpart) (docker-compose.yml:535,537,553-555,599,627-628).
  • BR-SUPA-06 (Hardcoded Form URLs in Edge Runtime): PROD Edge Runtime hardcodes HOSTINGER_WRITER_URL=https://dealflow.planovi.app/api/form-writer.php and FORM_BASE_URL=https://dealflow.planovi.app/offer.php; DEV hardcodes dev-dealflow.planovi.app, PUBLIC_SUPABASE_URL=https://dev-api.planovi.app, and SIGNING_PAGE_URL=https://dev-agreements.planovi.app/sign.php (docker-compose.yml:141-142,543-544,551-552). Both set INVITE_LINK_MODE: token_hash.
  • BR-SUPA-07 (Kong Route Path Preservation): All 4 Kong routes in proxy/kong.yml:14,22,30,38 set strip_path: false and KONG_DNS_ORDER: LAST,A,CNAME (docker-compose.yml:74,476). Note: proxy/kong.yml does NOT define a /realtime/v1/ service/route in Git, whereas live VPS kong-dev.yml may differ.
  • BR-SUPA-08 (GoTrue CORS Headers): GoTrue explicitly sets GOTRUE_CORS_ALLOWED_HEADERS: apikey,x-client-info,x-supabase-api-version (docker-compose.yml:201,606).

State Machines & Data Lifecycle

  • Database & Storage persistence lifecycle: managed by Docker named volumes (supabase-db-data, supabase-storage-data, supabase-db-data-dev, supabase-storage-data-dev) surviving container recreations (docker compose up -d). Edge Functions code is ephemeral host-bind-mounted (./supabase/functions-{prod,dev}) and overwritten on deploy via rsync --delete.

Technical Contract

  • Internal Ports (vps-network only, none host-published): PostgreSQL 5432, Kong 8000, Studio 3000, Edge Runtime 9000, pg-meta 8080, GoTrue 9999, Storage 5000, PostgREST 3000.
  • External Integrations: smtp.resend.com:587 (SMTP AUTH), Google OAuth 2.0, Stripe API, Gemini API, InCharge API, dealflow.planovi.app/dev-dealflow.planovi.app.

Cross-Repository Dependencies

  • planovi-backend -> supabase-edge-runtime-{prod,dev} | Deployed via vps-deployer rsync into ./supabase/functions-{prod,dev}.
  • planovi-flutter-app -> api.planovi.app / dev-api.planovi.app | HTTPS + JWT/apikey | Calls /rest/v1/, /auth/v1/, /storage/v1/, /functions/v1/.
  • supabase-edge-runtime-{prod,dev} -> planovi-microservices | HTTPS + X-Api-Secret (DEALFLOW_API_SECRET / DEALFLOW_API_SECRET_DEV) | POST /api/form-writer.php.
  • supabase-auth / supabase-auth-dev -> planovi-microservices (via vps-proxy:8080) | Internal HTTP | Fetches HTML email templates.

Why It Works This Way

  • Shared JWT_SECRET / POSTGRES_PASSWORD across PROD & DEV: Simplifies single-.env management on one VPS, while OAuth (8428246) and Dealflow/Scheduler secrets (b3a7ad3) were later split via *_DEV overrides when DEV credentials diverged from PROD.
  • DATABASE_MIGRATIONS_RUN: "false" on Storage: Prevents supabase-storage from colliding with the storage schema already migrated/dumped from Supabase Cloud.
  • [TEAM_WHY_NEEDED: Should PROD and DEV eventually use separate JWT_SECRET, POSTGRES_PASSWORD, STRIPE_SECRET_KEY, and STRIPE_WEBHOOK_SECRET keys to prevent cross-environment token reuse or Stripe test/live collisions?]
  • [TEAM_WHY_NEEDED: Why are PUBLIC_SUPABASE_URL, SIGNING_PAGE_URL, and SIGNING_SECRET configured on supabase-edge-runtime-dev (docker-compose.yml:551-553) but omitted from supabase-edge-runtime (PROD)?]
  • [TEAM_WHY_NEEDED: Why is proxy/kong-dev.yml mounted by docker-compose.yml:478 but not committed to git, and why does proxy/kong.yml omit /realtime/v1/ even though Flutter uses Realtime websockets?]

Debugging, Logs & Common Failure Modes

  • Symptom: create-form-preview or send-onboarding-invitation fails with HTTP 502 / HTTP 401 {"error":"Unauthorized"} -> Root Cause: DEALFLOW_API_SECRET_DEV in /opt/vps-stack/.env does not match /opt/vps-apps/microservices/dev/.env -> Verify: docker exec supabase-edge-runtime-dev env | grep DEALFLOW_API_SECRET vs grep DEALFLOW_API_SECRET /opt/vps-apps/microservices/dev/.env -> Fix: Align DEALFLOW_API_SECRET_DEV in /opt/vps-stack/.env and run docker compose up -d supabase-edge-runtime-dev.
  • Symptom: Google OAuth fails with redirect_uri_mismatch on DEV -> Root Cause: GOOGLE_CLIENT_ID_DEV/GOOGLE_CLIENT_SECRET_DEV missing or callback https://dev-api.planovi.app/auth/v1/callback not registered in GCP -> Verify: docker logs --tail 50 supabase-auth-dev -> Fix: Set GOOGLE_CLIENT_ID_DEV & GOOGLE_CLIENT_SECRET_DEV in /opt/vps-stack/.env and recreate supabase-auth-dev & supabase-edge-runtime-dev.
  • Symptom: Supabase Studio shows empty Edge Functions list -> Root Cause: docker-compose.yml:109-110,511-512 mounts ./supabase/functions instead of ./supabase/functions-prod / ./supabase/functions-dev -> Verify: ls -la /opt/vps-stack/supabase/ -> Fix: Update Studio volume mounts to environment-specific directories.