DocHub
Central Express.js service handling auth, API proxying, billing, monitoring, and admin

Gateway Service

Purpose

The gateway is the single entry point for all authenticated traffic. It runs as a systemd-managed Node.js process on port 3000 (localhost only, fronted by nginx). It handles user authentication, session management, API request proxying to the correct slice, Stripe billing webhooks, health monitoring, and admin endpoints.

Architecture

Source Files

File Purpose
src/index.ts Express app, middleware stack, route mounting, WebSocket upgrade handler
src/auth.ts Register, login, logout, session management (/auth/*)
src/proxy.ts Session validation, API request proxying to slice port
src/admin.ts Dashboard API, slice management, telemetry endpoints (/admin/*)
src/billing.ts Stripe checkout, portal, webhook handling (/billing/*)
src/orchestrator.ts Slice provisioning and destruction API (/orchestrator/*)
src/provisioner.ts Core provisioning logic — creates DB, schema, Docker container
src/monitor.ts 60-second health monitoring loop, telemetry collection
src/health.ts Gateway health check endpoint (/health)
src/database.ts PostgreSQL connection pool (max 20 connections)

Route Mounting Order

  1. POST /billing/webhook — Stripe raw body handler (before JSON parser)
  2. Middleware: helmet, morgan, cors, express.json, cookieParser
  3. /auth — Authentication routes
  4. /health — Health check (no auth)
  5. /admin — Admin dashboard (session or API key)
  6. /orchestrator — Provisioning (API key only)
  7. /billing — Subscription management (session auth)
  8. /api — Proxied to user’s slice (session auth)
  9. /socket.io — WebSocket proxy to slice (session auth)

Proxy Logic

The proxy middleware:

  1. Reads the session cookie from the request
  2. Queries: session → user → slice_id → slice port
  3. Creates an http-proxy-middleware instance targeting http://127.0.0.1:{port}
  4. Forwards the request with the original URL path preserved

WebSocket upgrades use the same session-to-port resolution, handling the HTTP upgrade protocol.

Variables

Variable Purpose
NODE_ENV production
PORT 3000
DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASSWORD Gateway database connection
ADMIN_KEY API key for admin/orchestrator endpoints
SLICE_DB_PASSWORD Password for per-slice database connections
STRIPE_SECRET_KEY Stripe API (empty = billing disabled)
STRIPE_WEBHOOK_SECRET Stripe webhook verification
STRIPE_PRICE_ID Subscription price ID
APP_URL https://app.ipnoelp.io

API Endpoints

Authentication (/auth/*)

Endpoint Method Auth Body/Response
/auth/register POST None {email, password} → Sets session cookie, returns user + sliceAssigned
/auth/login POST None {email, password} → Sets session cookie, returns user + slice
/auth/logout POST Session Clears session cookie
/auth/me GET Session Returns user + slice info, extends session (rolling 30-day)

Admin (/admin/*)

Endpoint Method Auth Purpose
/admin/dashboard GET Session/Key Full overview: server stats, slices, users, app metrics
/admin/slice/:id GET Session/Key Slice drill-down: 24h telemetry, app metrics, container
/admin/telemetry/server GET Session/Key Server telemetry time-series (?hours=N, max 168)
/admin/telemetry/slices GET Session/Key Per-slice telemetry time-series
/admin/users GET Key List all users with slice info
/admin/slices POST Key Create slice. Body: {port}
/admin/slices/:id DELETE Key Delete slice
/admin/slices/:id/status POST Key Update status. Body: {status}

Orchestrator (/orchestrator/*)

Endpoint Method Purpose
/orchestrator/provision POST Provision new slice (DB + container + register)
/orchestrator/destroy/:sliceId POST Destroy slice (stop + drop DB + delete data)
/orchestrator/status GET All slices + containers + memory overview

Billing (/billing/*)

Endpoint Method Auth Purpose
/billing/create-checkout POST Session Generate Stripe Checkout URL
/billing/portal GET Session Stripe Customer Portal URL
/billing/status GET Session Current subscription status
/billing/webhook POST Stripe sig Webhook events (checkout, payment, cancellation)

Dependencies

express 4.x, cors, helmet, morgan, cookie-parser, cookie, http-proxy-middleware, pg, bcryptjs, stripe

Handshakes

From To Protocol Purpose
nginx Gateway HTTP :3000 Reverse proxy all API/auth/admin traffic
Gateway Slice HTTP :500X Proxy authenticated API requests
Gateway Slice WebSocket :500X Proxy Socket.io connections
Gateway PostgreSQL TCP :5432 Session/user/slice lookups (wank_saas)
Gateway Slice DBs TCP :5432 App metrics queries (wa_slice_N)
Gateway Docker CLI Container health, stats, start/stop
Stripe Gateway HTTPS webhook Payment events

Status

Deployed and running as systemd service wank-gateway.service. Auto-restarts on failure.