Skip to content

The open alternative to Salesforce, designed for AI.

Fix environment variable casting (#4855)

Félix Malfaitbffd73e39109
diff --git a/packages/twenty-server/scripts/utils.ts b/packages/twenty-server/scripts/utils.ts
index 3b2bea52591..99dbd4b5089 100644
--- a/packages/twenty-server/scripts/utils.ts
+++ b/packages/twenty-server/scripts/utils.ts
@@ -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) =>
diff --git a/packages/twenty-server/src/command/command.ts b/packages/twenty-server/src/command/command.ts
index 2ebc22bdedf..a099c5943a3 100644
--- a/packages/twenty-server/src/command/command.ts
+++ b/packages/twenty-server/src/command/command.ts
@@ -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,
});
diff --git a/packages/twenty-server/src/database/typeorm/core/core.datasource.ts b/packages/twenty-server/src/database/typeorm/core/core.datasource.ts
index 751442c6577..64f3e2394a9 100644
--- a/packages/twenty-server/src/database/typeorm/core/core.datasource.ts
+++ b/packages/twenty-server/src/database/typeorm/core/core.datasource.ts
@@ -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',
diff --git a/packages/twenty-server/src/database/typeorm/metadata/metadata.datasource.ts b/packages/twenty-server/src/database/typeorm/metadata/metadata.datasource.ts
index b6fb0018ddf..7fbe5e599dd 100644
--- a/packages/twenty-server/src/database/typeorm/metadata/metadata.datasource.ts
+++ b/packages/twenty-server/src/database/typeorm/metadata/metadata.datasource.ts
@@ -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',
diff --git a/packages/twenty-server/src/engine/integrations/environment/environment-variables.ts b/packages/twenty-server/src/engine/integrations/environment/environment-variables.ts
index 227f0d38ac9..f5ca7098534 100644
--- a/packages/twenty-server/src/engine/integrations/environment/environment-variables.ts
+++ b/packages/twenty-server/src/engine/integrations/environment/environment-variables.ts
@@ -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);
diff --git a/packages/twenty-server/src/main.ts b/packages/twenty-server/src/main.ts
index 25e6f3cff75..a4d51925341 100644
--- a/packages/twenty-server/src/main.ts
+++ b/packages/twenty-server/src/main.ts
@@ -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();
diff --git a/packages/twenty-server/src/queue-worker/queue-worker.ts b/packages/twenty-server/src/queue-worker/queue-worker.ts
index be9e6da912f..15e4e9f3a8d 100644
--- a/packages/twenty-server/src/queue-worker/queue-worker.ts
+++ b/packages/twenty-server/src/queue-worker/queue-worker.ts
@@ -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);
diff --git a/packages/twenty-server/src/utils/generate-front-config.ts b/packages/twenty-server/src/utils/generate-front-config.ts
index 7005d265548..5f389e72f50 100644
--- a/packages/twenty-server/src/utils/generate-front-config.ts
+++ b/packages/twenty-server/src/utils/generate-front-config.ts
@@ -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,
},
},
};