/** * 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 or if not already loaded if (typeof process !== 'undefined' && process.env.NODE_ENV !== 'production') { dotenv.config({ path: path.resolve(process.cwd(), '.env') }); } const getEnv = (key: string, defaultValue?: string): string | undefined => { if (typeof process === 'undefined') return defaultValue; return process.env[key] || 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('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) || [], }, woocommerce: { url: getEnv('WOOCOMMERCE_URL'), consumerKey: getEnv('WOOCOMMERCE_CONSUMER_KEY'), consumerSecret: getEnv('WOOCOMMERCE_CONSUMER_SECRET'), }, wordpress: { appPassword: getEnv('WORDPRESS_APP_PASSWORD'), }, } 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, }, }; }