logicsrc/apps/pwa/src/config.mjs
Anthony Ettinger 4ac750e65b
feat(pwa): move app.logicsrc.com from Turso/libSQL to Postgres via @profullstack/libsql-pg (#218)
Production reads DATABASE_URL (postgres://) through @profullstack/libsql-pg,
which keeps the @libsql/client surface; no query changed. A missing,
non-Postgres or leftover libsql:// URL fails at boot in production. Dev and
tests keep libSQL (file:/:memory:) as a devDependency. Postgres migrations in
src/migrations-pg/ (converted with libsql-pg convert-schema, same file names
so the copied _migrations ledger matches); migrate.mjs picks the dialect.
Tests run against Postgres with PWA_TEST_DATABASE_URL.

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-25 09:28:40 -07:00

67 lines
2.7 KiB
JavaScript

// Central config. Reads .env (if present) with zero deps, then process.env wins.
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
// Tiny .env loader — does not override anything already in the environment.
function loadEnv() {
const file = path.join(ROOT, ".env");
if (!fs.existsSync(file)) return;
for (const line of fs.readFileSync(file, "utf8").split("\n")) {
const m = /^\s*([A-Z0-9_]+)\s*=\s*(.*)\s*$/i.exec(line);
if (!m) continue;
const key = m[1];
if (process.env[key] !== undefined) continue;
let val = m[2];
if ((val.startsWith('"') && val.endsWith('"')) || (val.startsWith("'") && val.endsWith("'"))) {
val = val.slice(1, -1);
}
process.env[key] = val;
}
}
loadEnv();
const origin = (process.env.PUBLIC_ORIGIN || `http://localhost:${process.env.PORT || 8080}`).replace(/\/+$/, "");
const rpID = new URL(origin).hostname;
export const config = {
root: ROOT,
env: process.env.NODE_ENV || "development",
port: Number(process.env.PORT || 8080),
origin,
// WebAuthn relying party = this host.
rpID,
rpName: "LogicSRC",
sessionSecret: process.env.SESSION_SECRET || "dev-insecure-secret-change-me",
db: {
// Postgres (postgres://) in production, through @profullstack/libsql-pg; a
// local libSQL file or `:memory:` for dev and tests. DATABASE_URL wins. A
// leftover TURSO_DATABASE_URL is still read so that db.mjs can refuse it
// with a message that names it, rather than silently opening a file db.
url: process.env.DATABASE_URL || process.env.TURSO_DATABASE_URL || "file:./data/local.db",
authToken: process.env.DATABASE_AUTH_TOKEN || process.env.TURSO_AUTH_TOKEN || undefined,
},
resend: {
apiKey: process.env.RESEND_API_KEY || "",
from: process.env.CREDSHARE_EMAIL_FROM || process.env.RESEND_FROM || "LogicSRC <noreply@logicsrc.com>",
},
coinpay: {
apiBase: (process.env.COINPAY_API_BASE || "https://coinpayportal.com").replace(/\/+$/, ""),
businessId: process.env.COINPAY_BUSINESS_ID || "",
webhookSecret: process.env.COINPAY_WEBHOOK_SECRET || "",
oauth: {
authorizeUrl: process.env.COINPAY_OAUTH_AUTHORIZE_URL || "",
tokenUrl: process.env.COINPAY_OAUTH_TOKEN_URL || "",
userinfoUrl: process.env.COINPAY_OAUTH_USERINFO_URL || "",
clientId: process.env.COINPAY_OAUTH_CLIENT_ID || "",
redirectUri: `${origin}/auth/coinpay/callback`,
scope: process.env.COINPAY_OAUTH_SCOPE || "openid profile",
},
},
get coinpayLoginEnabled() {
return Boolean(this.coinpay.oauth.authorizeUrl && this.coinpay.oauth.clientId);
},
secure: (process.env.NODE_ENV || "development") === "production",
};