Fix environment variable casting (#4855)

Fixes #4628
This commit is contained in:
Félix Malfait
2024-04-05 18:15:47 +02:00
committed by GitHub
parent bbdb926687
commit bffd73e391
8 changed files with 21 additions and 37 deletions

View File

@ -1,17 +1,11 @@
import { ConfigService } from '@nestjs/config';
import console from 'console';
import { DataSource } from 'typeorm';
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
const environmentService = new EnvironmentService(new ConfigService());
export const connectionSource = new DataSource({
type: 'postgres',
logging: false,
url: environmentService.get('PG_DATABASE_URL'),
url: process.env.PG_DATABASE_URL,
});
export const camelToSnakeCase = (str) =>

View File

@ -1,11 +1,8 @@
import { ConfigService } from '@nestjs/config';
import { CommandFactory } from 'nest-commander';
import { filterException } from 'src/engine/utils/global-exception-handler.util';
import { ExceptionHandlerService } from 'src/engine/integrations/exception-handler/exception-handler.service';
import { LoggerService } from 'src/engine/integrations/logger/logger.service';
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
import { CommandModule } from './command.module';
@ -20,10 +17,8 @@ async function bootstrap() {
exceptionHandlerService.captureExceptions([err]);
};
const environmentService = new EnvironmentService(new ConfigService());
const app = await CommandFactory.createWithoutRunning(CommandModule, {
bufferLogs: environmentService.get('LOGGER_IS_BUFFER_ENABLED'),
bufferLogs: process.env.LOGGER_IS_BUFFER_ENABLED === 'true',
errorHandler,
serviceErrorHandler: errorHandler,
});

View File

@ -1,13 +1,11 @@
import { ConfigService } from '@nestjs/config';
import { TypeOrmModuleOptions } from '@nestjs/typeorm';
import { DataSource, DataSourceOptions } from 'typeorm';
import { config } from 'dotenv';
config();
const configService = new ConfigService();
export const typeORMCoreModuleOptions: TypeOrmModuleOptions = {
url: configService.get('PG_DATABASE_URL'),
url: process.env.PG_DATABASE_URL,
type: 'postgres',
logging: ['error'],
schema: 'core',

View File

@ -1,13 +1,11 @@
import { ConfigService } from '@nestjs/config';
import { TypeOrmModuleOptions } from '@nestjs/typeorm';
import { DataSource, DataSourceOptions } from 'typeorm';
import { config } from 'dotenv';
config();
const configService = new ConfigService();
export const typeORMMetadataModuleOptions: TypeOrmModuleOptions = {
url: configService.get('PG_DATABASE_URL'),
url: process.env.PG_DATABASE_URL,
type: 'postgres',
logging: ['error'],
schema: 'metadata',

View File

@ -262,12 +262,14 @@ export class EnvironmentVariables {
REDIS_HOST: string = '127.0.0.1';
@CastToPositiveNumber()
REDIS_PORT: number = 6379;
API_TOKEN_EXPIRES_IN: string = '100y';
SHORT_TERM_TOKEN_EXPIRES_IN: string = '5m';
@CastToBoolean()
MESSAGING_PROVIDER_GMAIL_ENABLED: boolean = false;
MESSAGING_PROVIDER_GMAIL_CALLBACK_URL: string;
@ -284,6 +286,7 @@ export class EnvironmentVariables {
EMAIL_SMTP_HOST: string;
@CastToPositiveNumber()
EMAIL_SMTP_PORT: number = 587;
EMAIL_SMTP_USER: string;
@ -292,14 +295,18 @@ export class EnvironmentVariables {
OPENROUTER_API_KEY: string;
@CastToPositiveNumber()
API_RATE_LIMITING_TTL: number = 100;
@CastToPositiveNumber()
API_RATE_LIMITING_LIMIT: number = 500;
CACHE_STORAGE_TYPE: string = 'memory';
@CastToPositiveNumber()
CACHE_STORAGE_TTL: number = 3600 * 24 * 7;
@CastToBoolean()
CALENDAR_PROVIDER_GOOGLE_ENABLED: boolean = false;
AUTH_GOOGLE_APIS_CALLBACK_URL: string;
@ -307,7 +314,9 @@ export class EnvironmentVariables {
CHROME_EXTENSION_REDIRECT_URL: string;
}
export const validate = (config: Record<string, unknown>) => {
export const validate = (
config: Record<string, unknown>,
): EnvironmentVariables => {
const validatedConfig = plainToClass(EnvironmentVariables, config);
const errors = validateSync(validatedConfig);

View File

@ -1,7 +1,6 @@
import { NestFactory } from '@nestjs/core';
import { ValidationPipe } from '@nestjs/common';
import { NestExpressApplication } from '@nestjs/platform-express';
import { ConfigService } from '@nestjs/config';
import * as Sentry from '@sentry/node';
import { graphqlUploadExpress } from 'graphql-upload';
@ -14,15 +13,13 @@ import { AppModule } from './app.module';
import { generateFrontConfig } from './utils/generate-front-config';
import { settings } from './engine/constants/settings';
import { LoggerService } from './engine/integrations/logger/logger.service';
import { EnvironmentService } from './engine/integrations/environment/environment.service';
const bootstrap = async () => {
const environmentService = new EnvironmentService(new ConfigService());
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
cors: true,
bufferLogs: environmentService.get('LOGGER_IS_BUFFER_ENABLED'),
bufferLogs: process.env.LOGGER_IS_BUFFER_ENABLED === 'true',
rawBody: true,
snapshot: environmentService.get('DEBUG_MODE'),
snapshot: process.env.DEBUG_MODE === 'true',
});
const logger = app.get(LoggerService);
@ -64,7 +61,7 @@ const bootstrap = async () => {
// Create the env-config.js of the front at runtime
generateFrontConfig();
await app.listen(app.get(EnvironmentService).get('PORT'));
await app.listen(process.env.PORT ?? 3000);
};
bootstrap();

View File

@ -1,5 +1,4 @@
import { NestFactory } from '@nestjs/core';
import { ConfigService } from '@nestjs/config';
import {
MessageQueueJob,
@ -14,17 +13,14 @@ import { MessageQueue } from 'src/engine/integrations/message-queue/message-queu
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
import { getJobClassName } from 'src/engine/integrations/message-queue/utils/get-job-class-name.util';
import { QueueWorkerModule } from 'src/queue-worker/queue-worker.module';
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
async function bootstrap() {
let exceptionHandlerService: ExceptionHandlerService | undefined;
let loggerService: LoggerService | undefined;
try {
const environmentService = new EnvironmentService(new ConfigService());
const app = await NestFactory.createApplicationContext(QueueWorkerModule, {
bufferLogs: environmentService.get('LOGGER_IS_BUFFER_ENABLED'),
bufferLogs: process.env.LOGGER_IS_BUFFER_ENABLED === 'true',
});
loggerService = app.get(LoggerService);

View File

@ -1,17 +1,14 @@
import { ConfigService } from '@nestjs/config';
import * as fs from 'fs';
import * as path from 'path';
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
const environmentService = new EnvironmentService(new ConfigService());
import { config } from 'dotenv';
config();
export function generateFrontConfig(): void {
const configObject = {
window: {
_env_: {
REACT_APP_SERVER_BASE_URL: environmentService.get('SERVER_URL'),
REACT_APP_SERVER_BASE_URL: process.env.SERVER_URL,
},
},
};