refactor(config): add env parsing helpers and tighten startup validation
This commit is contained in:
@@ -12,13 +12,24 @@ import { RedisModule } from './database/redis.module';
|
||||
import { WsModule } from './ws/ws.module';
|
||||
import { FriendsModule } from './friends/friends.module';
|
||||
import { DollsModule } from './dolls/dolls.module';
|
||||
import { parseRedisRequired } from './common/config/env.utils';
|
||||
|
||||
/**
|
||||
* Validates required environment variables.
|
||||
* Throws an error if any required variables are missing or invalid.
|
||||
* Returns the validated config.
|
||||
*/
|
||||
function validateEnvironment(config: Record<string, any>): Record<string, any> {
|
||||
function getOptionalEnvString(
|
||||
config: Record<string, unknown>,
|
||||
key: string,
|
||||
): string | undefined {
|
||||
const value = config[key];
|
||||
return typeof value === 'string' ? value : undefined;
|
||||
}
|
||||
|
||||
function validateEnvironment(
|
||||
config: Record<string, unknown>,
|
||||
): Record<string, unknown> {
|
||||
const requiredVars = ['JWT_SECRET', 'DATABASE_URL'];
|
||||
|
||||
const missingVars = requiredVars.filter((varName) => !config[varName]);
|
||||
@@ -30,10 +41,44 @@ function validateEnvironment(config: Record<string, any>): Record<string, any> {
|
||||
}
|
||||
|
||||
// Validate PORT if provided
|
||||
if (config.PORT && isNaN(Number(config.PORT))) {
|
||||
if (config.PORT !== undefined && !Number.isFinite(Number(config.PORT))) {
|
||||
throw new Error('PORT must be a valid number');
|
||||
}
|
||||
|
||||
if (config.NODE_ENV === 'production') {
|
||||
if (
|
||||
typeof config.JWT_SECRET !== 'string' ||
|
||||
config.JWT_SECRET.length < 32
|
||||
) {
|
||||
throw new Error(
|
||||
'JWT_SECRET must be at least 32 characters in production',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const redisRequired = parseRedisRequired({
|
||||
nodeEnv: getOptionalEnvString(config, 'NODE_ENV'),
|
||||
redisRequired: getOptionalEnvString(config, 'REDIS_REQUIRED'),
|
||||
});
|
||||
|
||||
if (redisRequired && !config.REDIS_HOST) {
|
||||
throw new Error(
|
||||
'REDIS_REQUIRED is enabled but REDIS_HOST is not configured',
|
||||
);
|
||||
}
|
||||
|
||||
const redisConnectTimeout = getOptionalEnvString(
|
||||
config,
|
||||
'REDIS_CONNECT_TIMEOUT_MS',
|
||||
);
|
||||
if (
|
||||
redisConnectTimeout !== undefined &&
|
||||
(!Number.isFinite(Number(redisConnectTimeout)) ||
|
||||
Number(redisConnectTimeout) <= 0)
|
||||
) {
|
||||
throw new Error('REDIS_CONNECT_TIMEOUT_MS must be a positive number');
|
||||
}
|
||||
|
||||
validateOptionalProvider(config, 'GOOGLE');
|
||||
validateOptionalProvider(config, 'DISCORD');
|
||||
|
||||
@@ -41,7 +86,7 @@ function validateEnvironment(config: Record<string, any>): Record<string, any> {
|
||||
}
|
||||
|
||||
function validateOptionalProvider(
|
||||
config: Record<string, any>,
|
||||
config: Record<string, unknown>,
|
||||
provider: 'GOOGLE' | 'DISCORD',
|
||||
): void {
|
||||
const vars = [
|
||||
|
||||
Reference in New Issue
Block a user