From f1cb3faacd61970eec0e375c8bf825cf935ec956 Mon Sep 17 00:00:00 2001 From: kienmai Date: Tue, 9 Apr 2024 00:47:22 -0500 Subject: [PATCH] Load env for config --- apps/backend/package.json | 1 + apps/backend/src/app.module.ts | 2 -- apps/backend/src/config.ts | 13 +++++++++++++ apps/backend/src/main.ts | 2 ++ 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/apps/backend/package.json b/apps/backend/package.json index cc3f110..e56cb83 100644 --- a/apps/backend/package.json +++ b/apps/backend/package.json @@ -31,6 +31,7 @@ "@team8/utils": "*", "@types/passport-jwt": "^4.0.1", "bcrypt": "^5.1.1", + "dotenv": "^16.4.5", "i": "^0.3.7", "mysql2": "^3.9.1", "passport": "^0.7.0", diff --git a/apps/backend/src/app.module.ts b/apps/backend/src/app.module.ts index 9912a75..da3d21c 100644 --- a/apps/backend/src/app.module.ts +++ b/apps/backend/src/app.module.ts @@ -3,7 +3,6 @@ import {ServeStaticModule} from '@nestjs/serve-static'; import {join} from 'path'; import {RoutesModule} from './routes.module'; import {ConfigModule} from '@nestjs/config'; -import {envFiles} from './config'; import {HealthModule} from './health/health.module'; import {AuthModule} from './auth/auth.module'; import {DatabaseModule} from './database/database.module'; @@ -17,7 +16,6 @@ import {DepartmentModule} from './department/department.module'; imports: [ ConfigModule.forRoot({ isGlobal: true, - envFilePath: envFiles('apps.env', 'database.env'), }), ServeStaticModule.forRoot({ rootPath: join(__dirname, '/../../frontend/dist'), diff --git a/apps/backend/src/config.ts b/apps/backend/src/config.ts index d269a45..0623db5 100644 --- a/apps/backend/src/config.ts +++ b/apps/backend/src/config.ts @@ -1,4 +1,17 @@ +import * as dotenv from 'dotenv'; + const env = process.env.NODE_ENV || 'dev'; export const envFiles = (...filenames: string[]): string[] => filenames.map((filename) => `${process.cwd()}/config/${env}/${filename}`); + +export const configFiles = [ + 'apps.env', + 'database.env', +]; + +export const loadEnv = () => { + envFiles(...configFiles).forEach((filename) => { + dotenv.config({path: filename}); + }); +}; diff --git a/apps/backend/src/main.ts b/apps/backend/src/main.ts index d27ffaf..965a3ad 100644 --- a/apps/backend/src/main.ts +++ b/apps/backend/src/main.ts @@ -2,11 +2,13 @@ import {NestFactory} from '@nestjs/core'; import {AppModule} from './app.module'; import {ConfigService} from '@nestjs/config'; import {ValidationPipe} from '@nestjs/common'; +import {loadEnv} from './config'; /** * Bootstraps the application. */ async function bootstrap() { + loadEnv(); const app = await NestFactory.create(AppModule); const configService = app.get(ConfigService); app.useGlobalPipes(