panels-origin/api/config.ts
Cursor Agent ab20701fea
api: ping R2 con ListObjects, no HeadBucket
Cloudflare R2 suele devolver 403 en HeadBucket con token acotado
al bucket, y eso tumba el fail-fast de arranque. El ping ahora
lista o escribe una sonda y registra el error S3.

Co-authored-by: alberto.martinez <alberto.martinez@mrdev.mx>
2026-09-03 05:42:13 +00:00

88 lines
4 KiB
TypeScript

import { join, dirname } from "node:path";
import { fileURLToPath } from "node:url";
const here = dirname(fileURLToPath(import.meta.url));
export const ROOT = join(here, "..");
export const DATA_DIR = join(ROOT, "data");
// Cache local de archivos descifrados/temporales (no la fuente de verdad --
// esa vive en Contabo Object Storage, ver api/storage.ts / Fase 4c).
export const TMP_DIR = join(DATA_DIR, "tmp");
function required(name: string): string {
const value = Deno.env.get(name);
if (!value) {
throw new Error(
`Falta la variable de entorno ${name}. No hay default inseguro -- ver .env.example.`,
);
}
return value;
}
const isDev = (Deno.env.get("DENO_ENV") ?? "development") !== "production";
/** SESSION_SECRET/DOCS_KEY nunca tienen default fuera de desarrollo: un
* default hardcodeado en el código es un secreto público. */
function requiredOrDevFallback(name: string, devFallback: string): string {
const value = Deno.env.get(name);
if (value) return value;
if (isDev) return devFallback;
throw new Error(`Falta la variable de entorno ${name} (obligatoria fuera de desarrollo).`);
}
export const config = {
port: Number(Deno.env.get("PORT") ?? "8000"),
sessionSecret: requiredOrDevFallback("SESSION_SECRET", "dev-session-secret-change-me"),
apiKey: Deno.env.get("API_KEY") ?? "",
docsKeyHex: requiredOrDevFallback(
"DOCS_KEY",
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
),
/** Solo vía env. Compose exige SEED_PASSWORD; no hay default en código. */
seedPassword: Deno.env.get("SEED_PASSWORD") ?? "",
vcardBase: Deno.env.get("VCARD_BASE") ?? "https://vcard.arctec.com.mx?info=",
panelLoginUrl: Deno.env.get("PANEL_LOGIN_URL") ?? "http://localhost:3000/login",
smtpHost: (Deno.env.get("SMTP_HOST") ?? "").trim(),
smtpPort: Number(Deno.env.get("SMTP_PORT") ?? "587"),
smtpUser: (Deno.env.get("SMTP_USER") ?? "").trim(),
smtpPass: Deno.env.get("SMTP_PASS") ?? "",
/** Compose puede pasar SMTP_FROM vacío; no pisar el default. */
smtpFrom: (Deno.env.get("SMTP_FROM") ?? "").trim() || "PANELS <noreply@panel.local>",
/** En HTTPS (Coolify) poner COOKIE_SECURE=true */
cookieSecure: ["1", "true", "yes"].includes(
(Deno.env.get("COOKIE_SECURE") ?? "").toLowerCase(),
),
// --- Postgres: panels_platform (control plane SaaS, base separada) ---
databaseUrlPlatform: isDev
? (Deno.env.get("DATABASE_URL_PLATFORM") ?? "")
: required("DATABASE_URL_PLATFORM"),
// --- Postgres: panels_product, esquema iam (identidad del tenant) ---
databaseUrlIam: isDev
? (Deno.env.get("DATABASE_URL_IAM") ?? "")
: required("DATABASE_URL_IAM"),
// Credenciales _owner: SOLO para resolver el login por username (ver
// api/iam_db.ts#getAuthPool) -- el dueño de la tabla no está sujeto a
// Row Level Security, necesario porque el username es único globalmente
// y hay que ubicarlo antes de conocer su tenant_id.
databaseUrlIamOwner: Deno.env.get("DATABASE_URL_IAM_OWNER") ?? "",
// --- Postgres: panels_product, esquema core (negocio) ---
databaseUrlCore: isDev
? (Deno.env.get("DATABASE_URL_CORE") ?? "")
: required("DATABASE_URL_CORE"),
// Credenciales _owner: solo para scripts (bootstrap/ETL) y resoluciones
// internas puntuales que cruzan tenants a propósito (ver api/db.ts#getCoreDb).
databaseUrlCoreOwner: Deno.env.get("DATABASE_URL_CORE_OWNER") ?? "",
// --- Redis: sesiones (iam:*) y cache (core:*) ---
redisUrlIam: isDev ? (Deno.env.get("REDIS_URL_IAM") ?? "") : required("REDIS_URL_IAM"),
redisUrlCore: isDev ? (Deno.env.get("REDIS_URL_CORE") ?? "") : required("REDIS_URL_CORE"),
// --- Contabo Object Storage (S3-compatible) -- expedientes/PDFs/logos ---
s3Endpoint: (Deno.env.get("S3_ENDPOINT") ?? "").trim().replace(/\/$/, ""),
s3Bucket: (Deno.env.get("S3_BUCKET") ?? "").trim(),
s3Region: (Deno.env.get("S3_REGION") ?? "auto").trim() || "auto",
s3AccessKeyId: (Deno.env.get("S3_ACCESS_KEY_ID") ?? "").trim(),
s3SecretAccessKey: Deno.env.get("S3_SECRET_ACCESS_KEY") ?? "",
isDev,
};