/** * Centralized configuration management for the application. * This file defines the schema and provides a type-safe way to access environment variables. */ import dotenv from 'dotenv'; import path from 'path'; // Load .env file in development (server-side only) if (typeof window === 'undefined' && process.env.NODE_ENV !== 'production') { dotenv.config({ path: path.resolve(process.cwd(), '.env') }); } const getEnv = (key: string, defaultValue?: string): string | undefined => { // In the browser, we can only access NEXT_PUBLIC_ variables if (typeof window !== 'undefined') { if (!key.startsWith('NEXT_PUBLIC_')) { return defaultValue; } return (process.env as any)[key] || defaultValue; } if (typeof process === 'undefined') return defaultValue; // In Docker/Production, variables are in process.env // In local development, they might be in .env const value = process.env[key]; // Check for quoted values (common when passed via SSH/Docker) if (typeof value === 'string') { const trimmed = value.trim(); if ((trimmed.startsWith("'") && trimmed.endsWith("'")) || (trimmed.startsWith('"') && trimmed.endsWith('"'))) { return trimmed.slice(1, -1); } return trimmed; } return defaultValue; }; export const config = { env: getEnv('NODE_ENV', 'development'), isProduction: getEnv('NODE_ENV') === 'production', isDevelopment: getEnv('NODE_ENV') === 'development', isTest: getEnv('NODE_ENV') === 'test', baseUrl: getEnv('NEXT_PUBLIC_BASE_URL', 'http://localhost:3000'), analytics: { umami: { websiteId: getEnv('NEXT_PUBLIC_UMAMI_WEBSITE_ID'), scriptUrl: getEnv('NEXT_PUBLIC_UMAMI_SCRIPT_URL', 'https://analytics.infra.mintel.me/script.js'), // The proxied path used in the frontend proxyPath: '/stats/script.js', enabled: Boolean(getEnv('NEXT_PUBLIC_UMAMI_WEBSITE_ID')), }, }, errors: { glitchtip: { // Use SENTRY_DSN for both server and client (proxied) dsn: getEnv('SENTRY_DSN'), // The proxied origin used in the frontend proxyPath: '/errors', enabled: Boolean(getEnv('SENTRY_DSN')), }, }, cache: { redis: { url: getEnv('REDIS_URL'), keyPrefix: getEnv('REDIS_KEY_PREFIX', 'klz:'), enabled: Boolean(getEnv('REDIS_URL')), }, }, logging: { level: getEnv('LOG_LEVEL', 'info'), }, mail: { host: getEnv('MAIL_HOST'), port: parseInt(getEnv('MAIL_PORT', '587')!, 10), user: getEnv('MAIL_USERNAME'), pass: getEnv('MAIL_PASSWORD'), from: getEnv('MAIL_FROM'), recipients: getEnv('MAIL_RECIPIENTS', '')?.split(',').filter(Boolean) || [], }, } as const; /** * Helper to get a masked version of the config for logging. */ export function getMaskedConfig() { const mask = (val: string | undefined) => (val ? `***${val.slice(-4)}` : 'not set'); return { env: config.env, baseUrl: config.baseUrl, analytics: { umami: { websiteId: mask(config.analytics.umami.websiteId), scriptUrl: config.analytics.umami.scriptUrl, enabled: config.analytics.umami.enabled, }, }, errors: { glitchtip: { dsn: mask(config.errors.glitchtip.dsn), enabled: config.errors.glitchtip.enabled, }, }, cache: { redis: { url: mask(config.cache.redis.url), keyPrefix: config.cache.redis.keyPrefix, enabled: config.cache.redis.enabled, }, }, logging: { level: config.logging.level, }, mail: { host: config.mail.host, port: config.mail.port, user: mask(config.mail.user), from: config.mail.from, recipients: config.mail.recipients, }, }; } /** * Validates that all required environment variables are set. * Should be called on server startup. */ export function validateConfig() { if (config.isProduction && typeof window === 'undefined') { const required = [ 'NEXT_PUBLIC_BASE_URL', ]; for (const key of required) { const value = getEnv(key); if (!value) { // In Next.js, process.env might not be a real object with enumerable keys in the bundle // so we check the specific key directly const rawValue = process.env[key]; throw new Error(`Missing required environment variable: ${key}. (getEnv: "${value}", process.env: "${rawValue}"). If this is set in your environment, ensure it is not being shadowed or cleared by the container runtime.`); } } } }