This commit is contained in:
100
lib/config.ts
100
lib/config.ts
@@ -1,90 +1,56 @@
|
||||
/**
|
||||
* Centralized configuration management for the application.
|
||||
* This file defines the schema and provides a type-safe way to access environment variables.
|
||||
* This file 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;
|
||||
};
|
||||
import { env } from './env';
|
||||
|
||||
export const config = {
|
||||
env: getEnv('NODE_ENV', 'development'),
|
||||
isProduction: getEnv('NODE_ENV') === 'production',
|
||||
isDevelopment: getEnv('NODE_ENV') === 'development',
|
||||
isTest: getEnv('NODE_ENV') === 'test',
|
||||
env: env.NODE_ENV,
|
||||
isProduction: env.NODE_ENV === 'production',
|
||||
isDevelopment: env.NODE_ENV === 'development',
|
||||
isTest: env.NODE_ENV === 'test',
|
||||
|
||||
baseUrl: getEnv('NEXT_PUBLIC_BASE_URL', 'http://localhost:3000'),
|
||||
baseUrl: env.NEXT_PUBLIC_BASE_URL,
|
||||
|
||||
analytics: {
|
||||
umami: {
|
||||
websiteId: getEnv('NEXT_PUBLIC_UMAMI_WEBSITE_ID'),
|
||||
scriptUrl: getEnv('NEXT_PUBLIC_UMAMI_SCRIPT_URL', 'https://analytics.infra.mintel.me/script.js'),
|
||||
websiteId: env.NEXT_PUBLIC_UMAMI_WEBSITE_ID,
|
||||
scriptUrl: env.NEXT_PUBLIC_UMAMI_SCRIPT_URL,
|
||||
// The proxied path used in the frontend
|
||||
proxyPath: '/stats/script.js',
|
||||
enabled: Boolean(getEnv('NEXT_PUBLIC_UMAMI_WEBSITE_ID')),
|
||||
enabled: Boolean(env.NEXT_PUBLIC_UMAMI_WEBSITE_ID),
|
||||
},
|
||||
},
|
||||
|
||||
errors: {
|
||||
glitchtip: {
|
||||
// Use SENTRY_DSN for both server and client (proxied)
|
||||
dsn: getEnv('SENTRY_DSN'),
|
||||
dsn: env.SENTRY_DSN,
|
||||
// The proxied origin used in the frontend
|
||||
proxyPath: '/errors',
|
||||
enabled: Boolean(getEnv('SENTRY_DSN')),
|
||||
enabled: Boolean(env.SENTRY_DSN),
|
||||
},
|
||||
},
|
||||
|
||||
cache: {
|
||||
redis: {
|
||||
url: getEnv('REDIS_URL'),
|
||||
keyPrefix: getEnv('REDIS_KEY_PREFIX', 'klz:'),
|
||||
enabled: Boolean(getEnv('REDIS_URL')),
|
||||
url: env.REDIS_URL,
|
||||
keyPrefix: env.REDIS_KEY_PREFIX,
|
||||
enabled: Boolean(env.REDIS_URL),
|
||||
},
|
||||
},
|
||||
|
||||
logging: {
|
||||
level: getEnv('LOG_LEVEL', 'info'),
|
||||
level: env.LOG_LEVEL,
|
||||
},
|
||||
|
||||
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) || [],
|
||||
host: env.MAIL_HOST,
|
||||
port: env.MAIL_PORT,
|
||||
user: env.MAIL_USERNAME,
|
||||
pass: env.MAIL_PASSWORD,
|
||||
from: env.MAIL_FROM,
|
||||
recipients: env.MAIL_RECIPIENTS,
|
||||
},
|
||||
} as const;
|
||||
|
||||
@@ -129,25 +95,3 @@ export function getMaskedConfig() {
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user