env
All checks were successful
Build & Deploy KLZ Cables / build-and-deploy (push) Successful in 3m52s

This commit is contained in:
2026-01-28 00:41:29 +01:00
parent 8242687b07
commit 2896556659
2 changed files with 28 additions and 23 deletions

View File

@@ -2,6 +2,13 @@
* 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
@@ -31,21 +38,6 @@ const getEnv = (key: string, defaultValue?: string): string | undefined => {
return defaultValue;
};
const isProduction = getEnv('NODE_ENV') === 'production';
// Required variables in production
if (isProduction && typeof window === 'undefined') {
const required = [
'NEXT_PUBLIC_BASE_URL',
];
for (const key of required) {
if (!getEnv(key)) {
throw new Error(`Missing required environment variable: ${key}`);
}
}
}
export const config = {
env: getEnv('NODE_ENV', 'development'),
isProduction: getEnv('NODE_ENV') === 'production',
@@ -137,3 +129,21 @@ 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) {
if (!getEnv(key)) {
throw new Error(`Missing required environment variable: ${key}`);
}
}
}
}

View File

@@ -6,14 +6,9 @@ import { RedisCacheService } from './cache/redis-cache-service';
import { GlitchtipErrorReportingService } from './errors/glitchtip-error-reporting-service';
import { NoopErrorReportingService } from './errors/noop-error-reporting-service';
import { PinoLoggerService } from './logging/pino-logger-service';
import { config, getMaskedConfig } from '../config';
import dotenv from 'dotenv';
import path from 'path';
// Load .env file in development
if (process.env.NODE_ENV !== 'production') {
dotenv.config({ path: path.resolve(process.cwd(), '.env') });
}
import { config, getMaskedConfig, validateConfig } from '../config';
// Validate configuration on server startup
validateConfig();
let singleton: AppServices | undefined;