From 28965566594cda6211088d518e414f481d677dbe Mon Sep 17 00:00:00 2001 From: Marc Mintel Date: Wed, 28 Jan 2026 00:41:29 +0100 Subject: [PATCH] env --- lib/config.ts | 40 ++++++++++++++++---------- lib/services/create-services.server.ts | 11 ++----- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/lib/config.ts b/lib/config.ts index a98a281b..df5a9639 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -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}`); + } + } + } +} diff --git a/lib/services/create-services.server.ts b/lib/services/create-services.server.ts index d6d758e9..b1960242 100644 --- a/lib/services/create-services.server.ts +++ b/lib/services/create-services.server.ts @@ -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;