73 lines
2.8 KiB
JavaScript
73 lines
2.8 KiB
JavaScript
// Centralized environment loader
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
import dotenv from 'dotenv';
|
|
import fs from 'fs';
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
// Versuche zuerst Produktions-.env, dann lokale .env
|
|
const productionEnvPath = '/opt/yourpart/backend/.env';
|
|
const localEnvPath = path.resolve(__dirname, '../.env');
|
|
|
|
let envPath = localEnvPath; // Fallback
|
|
let usingProduction = false;
|
|
if (fs.existsSync(productionEnvPath)) {
|
|
// Prüfe Lesbarkeit bevor wir versuchen, sie zu laden
|
|
try {
|
|
fs.accessSync(productionEnvPath, fs.constants.R_OK);
|
|
envPath = productionEnvPath;
|
|
usingProduction = true;
|
|
console.log('[env] Produktions-.env gefunden und lesbar:', productionEnvPath);
|
|
} catch (err) {
|
|
console.warn('[env] Produktions-.env vorhanden, aber nicht lesbar - verwende lokale .env stattdessen:', productionEnvPath);
|
|
console.warn('[env] Fehler:', err && err.message);
|
|
envPath = localEnvPath;
|
|
}
|
|
} else {
|
|
console.log('[env] Produktions-.env nicht gefunden, lade lokale .env:', localEnvPath);
|
|
}
|
|
|
|
// Lade .env-Datei (robust gegen Fehler)
|
|
console.log('[env] Versuche .env zu laden von:', envPath);
|
|
console.log('[env] Datei existiert:', fs.existsSync(envPath));
|
|
let result;
|
|
try {
|
|
result = dotenv.config({ path: envPath });
|
|
if (result.error) {
|
|
console.warn('[env] Konnte .env nicht laden:', result.error.message);
|
|
console.warn('[env] Fehler-Details:', result.error);
|
|
} else {
|
|
console.log('[env] .env erfolgreich geladen von:', envPath, usingProduction ? '(production)' : '(local)');
|
|
console.log('[env] Geladene Variablen:', Object.keys(result.parsed || {}));
|
|
}
|
|
} catch (err) {
|
|
// Sollte nicht passieren, aber falls dotenv intern eine Exception wirft (z.B. EACCES), fange sie ab
|
|
console.warn('[env] Unerwarteter Fehler beim Laden der .env:', err && err.message);
|
|
console.warn('[env] Stack:', err && err.stack);
|
|
if (envPath !== localEnvPath && fs.existsSync(localEnvPath)) {
|
|
console.log('[env] Versuche stattdessen lokale .env:', localEnvPath);
|
|
try {
|
|
result = dotenv.config({ path: localEnvPath });
|
|
if (!result.error) {
|
|
console.log('[env] Lokale .env erfolgreich geladen von:', localEnvPath);
|
|
}
|
|
} catch (err2) {
|
|
console.warn('[env] Konnte lokale .env auch nicht laden:', err2 && err2.message);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Debug: Zeige Redis-Konfiguration
|
|
console.log('[env] Redis-Konfiguration:');
|
|
console.log('[env] REDIS_HOST:', process.env.REDIS_HOST);
|
|
console.log('[env] REDIS_PORT:', process.env.REDIS_PORT);
|
|
console.log('[env] REDIS_PASSWORD:', process.env.REDIS_PASSWORD ? '***gesetzt***' : 'NICHT GESETZT');
|
|
console.log('[env] REDIS_URL:', process.env.REDIS_URL);
|
|
|
|
if (!process.env.SECRET_KEY) {
|
|
console.warn('[env] SECRET_KEY nicht gesetzt in .env');
|
|
}
|
|
export {};
|