Skip to content

Commit

Permalink
Refactor/env (#15)
Browse files Browse the repository at this point in the history
* Refactor: Edit getEnvPath

- 서버에서 실행할 때, (staging, production)에는 env파일에서 로딩하지 않도록 수정

* Refactor: Set ignoreEnvFile

환경에 따라 서버에서 실행되는 경우 ignoreEnvFile==true 가 되도록 설정

* Refactor: refactor get NODE_ENV

NODE_ENV를 한번만 로드하고 사용할 수 있도록 수정
- 중복 코드 제거

Refactor: refactor get NODE_ENV

Refactor: refactor get NODE_ENV
  • Loading branch information
sally0226 authored Jun 29, 2024
1 parent 0537c53 commit 081d6e7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { MikroOrmModule } from '@mikro-orm/nestjs';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthModule } from './auth/auth.module';
import { getEnvPath } from './common/helper/env.helper';
import { getNodeEnv, isIgnoreEnvFile } from './common/helper/env.helper';
import { envValidation } from './common/helper/env.validation';
import { MapModule } from './map/map.module';
import { UserMapModule } from './user-map/user-map.module';
Expand All @@ -16,9 +16,10 @@ import { UserModule } from './user/user.module';
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: getEnvPath(`${__dirname}/..`),
envFilePath: `${__dirname}/../.${getNodeEnv}.env`,
cache: true,
validate: envValidation,
ignoreEnvFile: isIgnoreEnvFile,
}),
MikroOrmModule.forRoot(),
AuthModule,
Expand Down
21 changes: 15 additions & 6 deletions src/common/helper/env.helper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { resolve } from 'path';
import { InternalServerErrorException } from '@nestjs/common';

export const getEnvPath = (dest: string): string => {
const env: string = process.env.NODE_ENV;
const filename: string = env ? `.${env}.env` : '.development.env';
return resolve(`${dest}/${filename}`);
};
import { NODE_ENVIRONMENT } from './env.validation';

export const getNodeEnv = (() => {
const env = process.env.NODE_ENV;
const nodeEnv = NODE_ENVIRONMENT[env];
if (nodeEnv === undefined) {
throw new InternalServerErrorException('Unknown NODE_ENV');
}
return nodeEnv;
})();

export const isIgnoreEnvFile =
getNodeEnv === NODE_ENVIRONMENT.stage ||
getNodeEnv === NODE_ENVIRONMENT.production;
8 changes: 4 additions & 4 deletions src/common/helper/env.validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ const stage = 'stage';
const production = 'production';
const test = 'test';

const nodeEnvironment = {
export const NODE_ENVIRONMENT = {
[development]: development,
[stage]: stage,
[production]: production,
[test]: test,
};
} as const;

export class EnvironmentVariables {
@IsEnum(nodeEnvironment)
NODE_ENV: keyof typeof nodeEnvironment;
@IsEnum(NODE_ENVIRONMENT)
NODE_ENV: keyof typeof NODE_ENVIRONMENT;

@IsString()
DB_HOST: string;
Expand Down

0 comments on commit 081d6e7

Please sign in to comment.