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
- 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/withstrip_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).
- PROD (
- PostgreSQL 15 Databases (
supabase-db&supabase-db-dev):- Persistent Docker named volumes
supabase-db-data(PROD) andsupabase-db-data-dev(DEV). - Network aliases
db(PROD) anddb-dev(DEV) onvps-network. - Healthcheck:
pg_isready -U postgresevery5s, timeout5s,5retries (docker-compose.yml:56-60,458-462).
- Persistent Docker named volumes
- GoTrue Auth (
supabase-auth&supabase-auth-dev):- Connects to
supabase-db/supabase-db-devwith?search_path=auth. - Sends transactional auth emails via
smtp.resend.com:587(user: resend,pass: ${RESEND_API_KEY}, sender name"Planovi", rate limit200emails/hour). - Fetches 6 HTML templates dynamically from
http://vps-proxy:8080/email-templates/{confirm-signup,invite-user,magic-link,change-email,reset-password,reauthentication}.htmlwith 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).
- PROD:
- Connects to
- 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).
- Starts Deno main router via
- PostgREST & Storage API:
- PostgREST exposes schemas
public,storage,graphql_public,websitewith anon roleanon(docker-compose.yml:267-268,672-673). - Storage API (
v1.11.0) usesSTORAGE_BACKEND: filemounted tosupabase-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).
- PostgREST exposes schemas
- Supabase Studio & Postgres Meta:
- Studio connects to
supabase-pg-meta:8080/supabase-pg-meta-dev:8080and persists SQL snippets in./supabase/snippets-prodand./supabase/snippets-dev(docker-compose.yml:111,513).
- Studio connects to
- One-Time Cloud-to-VPS Storage Migration (
scripts/migrate-storage-files.py):- Queries
SELECT bucket_id, name FROM storage.objects;viadocker 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}.
- Queries
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 = 52428800bytes (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, andlocalhost:8000(with/**wildcards) and cap outbound auth emails atGOTRUE_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-datavssupabase-db-data-dev), storage volumes (supabase-storage-datavssupabase-storage-data-dev), and Edge Function directories (functions-prodvsfunctions-dev). However, both stacks share the samePOSTGRES_PASSWORD,JWT_SECRET,SUPABASE_ANON_KEY,SUPABASE_SERVICE_ROLE_KEY,RESEND_API_KEY,GEMINI_API_KEY,STRIPE_*keys, andDEALFLOW_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, andSIGNING_SECRET_DEVare split for DEV with${VAR_DEV:-${VAR}}fallback (exceptSIGNING_SECRET_DEVwhich 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.phpandFORM_BASE_URL=https://dealflow.planovi.app/offer.php; DEV hardcodesdev-dealflow.planovi.app,PUBLIC_SUPABASE_URL=https://dev-api.planovi.app, andSIGNING_PAGE_URL=https://dev-agreements.planovi.app/sign.php(docker-compose.yml:141-142,543-544,551-552). Both setINVITE_LINK_MODE: token_hash. - BR-SUPA-07 (Kong Route Path Preservation): All 4 Kong routes in
proxy/kong.yml:14,22,30,38setstrip_path: falseandKONG_DNS_ORDER: LAST,A,CNAME(docker-compose.yml:74,476). Note:proxy/kong.ymldoes NOT define a/realtime/v1/service/route in Git, whereas live VPSkong-dev.ymlmay 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 viarsync --delete.
Technical Contract
- Internal Ports (
vps-networkonly, none host-published): PostgreSQL5432, Kong8000, Studio3000, Edge Runtime9000, pg-meta8080, GoTrue9999, Storage5000, PostgREST3000. - 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 viavps-deployerrsyncinto./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_PASSWORDacross PROD & DEV: Simplifies single-.envmanagement on one VPS, while OAuth (8428246) and Dealflow/Scheduler secrets (b3a7ad3) were later split via*_DEVoverrides when DEV credentials diverged from PROD. DATABASE_MIGRATIONS_RUN: "false"on Storage: Preventssupabase-storagefrom colliding with thestorageschema 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-previeworsend-onboarding-invitationfails withHTTP 502/HTTP 401 {"error":"Unauthorized"}-> Root Cause:DEALFLOW_API_SECRET_DEVin/opt/vps-stack/.envdoes not match/opt/vps-apps/microservices/dev/.env-> Verify:docker exec supabase-edge-runtime-dev env | grep DEALFLOW_API_SECRETvsgrep DEALFLOW_API_SECRET /opt/vps-apps/microservices/dev/.env-> Fix: AlignDEALFLOW_API_SECRET_DEVin/opt/vps-stack/.envand rundocker compose up -d supabase-edge-runtime-dev. - Symptom: Google OAuth fails with
redirect_uri_mismatchon DEV -> Root Cause:GOOGLE_CLIENT_ID_DEV/GOOGLE_CLIENT_SECRET_DEVmissing or callbackhttps://dev-api.planovi.app/auth/v1/callbacknot registered in GCP -> Verify:docker logs --tail 50 supabase-auth-dev-> Fix: SetGOOGLE_CLIENT_ID_DEV&GOOGLE_CLIENT_SECRET_DEVin/opt/vps-stack/.envand recreatesupabase-auth-dev&supabase-edge-runtime-dev. - Symptom: Supabase Studio shows empty Edge Functions list -> Root Cause:
docker-compose.yml:109-110,511-512mounts./supabase/functionsinstead of./supabase/functions-prod/./supabase/functions-dev-> Verify:ls -la /opt/vps-stack/supabase/-> Fix: Update Studio volume mounts to environment-specific directories.