Files
KSP-MissionControl/packages/db/src/config.ts
T
Mavis a457b9d96f
CI / Lint, typecheck, test, build (pull_request) Failing after 10s
Phase 1: data pipeline (Postgres+Redis with in-memory fallback, mock telemetry publisher, WebSocket fan-out, hub /debug page)
- packages/db: postgres.js + ioredis wrapper, StateStore interface with
  InMemory + Postgres implementations, schema migration runner
- apps/api: refactored to use @kerbal-rt/db; live WebSocket hub subscribes
  to store changes and broadcasts to all clients
- apps/tools/mock-telemetry: Node script that generates realistic KSP
  state and POSTs to /api/v1/ingest at 1Hz (uses same keplerian math
  as the live-map renderer)
- apps/hub: new /debug page that connects to /api/v1/live and shows
  the live state
- ksp/README.md: documents Phase 1c (real kRPC bridge) and the two
  implementation options
- Tests: 17 total (5 kepler math, 5 db store, 5 API health/state,
  2 API WebSocket fan-out)

End-to-end verified: mock publisher → API → hub /debug page,
11 snapshots/5s over WebSocket, vessels advancing in mean anomaly.
2026-06-02 18:54:46 +00:00

46 lines
1.3 KiB
TypeScript

/**
* Configuration helpers for the db package.
*
* Reads from environment variables. Throws clearly if a required var
* is missing.
*/
export interface DbConfig {
/** Postgres connection URL, e.g. postgres://user:pass@host:5432/db */
databaseUrl: string;
/** Redis connection URL, e.g. redis://host:6379 */
redisUrl: string;
/** When true, callers should use the in-memory fallback instead. */
useInMemory: boolean;
}
const REQUIRED_FOR_DB = ['DATABASE_URL', 'REDIS_URL'] as const;
export function loadConfig(env: NodeJS.ProcessEnv = process.env): DbConfig {
const databaseUrl = env.DATABASE_URL;
const redisUrl = env.REDIS_URL;
// Allow opt-out for dev (e.g. when Docker isn't available)
const forceInMemory = env.USE_IN_MEMORY === '1' || env.USE_IN_MEMORY === 'true';
if (forceInMemory) {
return { databaseUrl: '', redisUrl: '', useInMemory: true };
}
const missing = REQUIRED_FOR_DB.filter((k) => !env[k]);
if (missing.length > 0) {
// Don't crash on import — let the caller decide. The API's
// bootstrap() logs a warning and falls back to in-memory mode.
return {
databaseUrl: databaseUrl ?? '',
redisUrl: redisUrl ?? '',
useInMemory: true,
};
}
return {
databaseUrl: databaseUrl!,
redisUrl: redisUrl!,
useInMemory: false,
};
}