From 33352958f4a59ee96ac536b4bbd1b493df3950de Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Sun, 31 Jul 2022 00:17:39 +0200 Subject: [PATCH] Refactor env variables access --- apps/api/src/main.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index fd8237cd2..b8bfb81de 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -1,13 +1,21 @@ import { Logger, ValidationPipe, VersioningType } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; import { NestFactory } from '@nestjs/core'; import { AppModule } from './app/app.module'; import { environment } from './environments/environment'; async function bootstrap() { + const configApp = await NestFactory.create(AppModule); + const configService = configApp.get(ConfigService); + + const NODE_ENV = + configService.get<'development' | 'production'>('NODE_ENV') ?? + 'development'; + const app = await NestFactory.create(AppModule, { logger: - process.env.NODE_ENV === 'production' + NODE_ENV === 'production' ? ['error', 'log', 'warn'] : ['debug', 'error', 'log', 'verbose', 'warn'] }); @@ -25,11 +33,11 @@ async function bootstrap() { }) ); - const host = process.env.HOST || '0.0.0.0'; - const port = process.env.PORT || 3333; - await app.listen(port, host, () => { + const HOST = configService.get('HOST') || '0.0.0.0'; + const PORT = configService.get('PORT') || 3333; + await app.listen(PORT, HOST, () => { logLogo(); - Logger.log(`Listening at http://${host}:${port}`); + Logger.log(`Listening at http://${HOST}:${PORT}`); Logger.log(''); }); }