Files
RAPPORT-HOST/server/env.js
T
karim 6a2393301d feat(admin): Betreiber-Panel (/api/admin) mit is_admin-Flag
- 0003_admin.sql: accounts.is_admin
- auth.js: ensureAdminFlag (Konto = ADMIN_EMAIL wird auto-promoted),
  is_admin im JWT, requireAdmin-Middleware (prüft DB autoritativ)
- routes/admin.js: GET /stats (Kunden/Abos/Instanzen/MRR), GET /accounts,
  GET /accounts/:id/instances, POST /instances/:id/{suspend,reactivate}
- register/login + /account/me liefern is_admin
- ADMIN_EMAIL in .env.example

E2E: Admin-Promotion, Kunde→403, Stats (2 Kunden/MRR 49), Kundenliste.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-05-31 00:04:19 +02:00

66 lines
2.9 KiB
JavaScript

// Zentrales Laden + Validieren der Umgebungsvariablen.
// Liest .env (einfacher Parser, keine Dependency) und fällt sonst auf
// process.env zurück — so läuft es lokal mit Datei und auf Hetzner mit
// echten Env-Vars, ohne Code-Änderung.
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const envPath = path.resolve(__dirname, "..", ".env");
if (fs.existsSync(envPath)) {
for (const line of fs.readFileSync(envPath, "utf8").split("\n")) {
const m = line.match(/^\s*([A-Z0-9_]+)\s*=\s*(.*)\s*$/);
if (!m) continue;
const key = m[1];
let val = m[2].replace(/^["']|["']$/g, "");
if (process.env[key] === undefined) process.env[key] = val;
}
}
const e = process.env;
export const env = {
port: parseInt(e.PORT || "8787", 10),
publicBaseUrl: (e.PUBLIC_BASE_URL || "http://localhost:8787").replace(/\/+$/, ""),
jwtSecret: e.JWT_SECRET || "dev-insecure-secret-change-me",
databaseUrl: e.DATABASE_URL || "postgres://rapport_host:rapport_host@localhost:55432/rapport_host",
// Konto mit dieser E-Mail wird automatisch zum Admin (Betreiber-Bereich /admin).
adminEmail: (e.ADMIN_EMAIL || "").trim().toLowerCase(),
// Gebautes public/ der RAPPORT-WEBSITE (Hugo). Default: Schwester-Repo lokal.
websitePublicDir: e.WEBSITE_PUBLIC_DIR ||
new URL("../../RAPPORT-WEBSITE/public", import.meta.url).pathname,
stripe: {
secretKey: e.STRIPE_SECRET_KEY || "",
webhookSecret: e.STRIPE_WEBHOOK_SECRET || "",
prices: {
solo: e.STRIPE_PRICE_SOLO || "",
studio: e.STRIPE_PRICE_STUDIO || "",
business: e.STRIPE_PRICE_BUSINESS || "",
},
},
rapport: {
apiUrl: e.RAPPORT_API_URL || "",
serviceKey: e.RAPPORT_SERVICE_KEY || "",
instanceUrlTemplate: e.RAPPORT_INSTANCE_URL_TEMPLATE || "http://localhost:8080/?studio={slug}",
},
};
// MOCK-Modus: ohne echte Stripe-/Rapport-Keys läuft alles lokal simuliert,
// damit der End-to-End-Flow ohne externe Dienste testbar ist.
// WICHTIG: Platzhalter-Werte aus .env.example (…CHANGE-ME) zählen als NICHT
// gesetzt — sonst würde ein sk_test_CHANGE-ME als echter Key durchgehen und
// echte Stripe-Calls mit ungültigem Key abfeuern (401 → Crash).
const isPlaceholder = (v) => !v || v.includes("CHANGE-ME");
export const stripeEnabled = !isPlaceholder(env.stripe.secretKey) && env.stripe.secretKey.startsWith("sk_");
export const provisioningMock = isPlaceholder(env.rapport.apiUrl) || isPlaceholder(env.rapport.serviceKey);
if (env.jwtSecret === "dev-insecure-secret-change-me") {
console.warn("⚠ JWT_SECRET nicht gesetzt — unsicheres Dev-Secret in Verwendung.");
}
if (!stripeEnabled) console.warn("⚠ STRIPE_SECRET_KEY fehlt — Billing läuft im MOCK-Modus.");
if (provisioningMock) console.warn("⚠ RAPPORT_API_URL/SERVICE_KEY fehlen — Provisioning läuft im MOCK-Modus.");