Skip to content

Commit

Permalink
Load env for config
Browse files Browse the repository at this point in the history
  • Loading branch information
maingockien01 committed Apr 9, 2024
1 parent 35a9e9e commit f1cb3fa
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 0 additions & 2 deletions apps/backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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'),
Expand Down
13 changes: 13 additions & 0 deletions apps/backend/src/config.ts
Original file line number Diff line number Diff line change
@@ -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});
});
};
2 changes: 2 additions & 0 deletions apps/backend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit f1cb3fa

Please sign in to comment.