Skip to content

Commit

Permalink
refactor: ♻️ prod, dev, local 을 모두 불러오도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
jpham005 committed Oct 18, 2023
1 parent 07a4eb4 commit 28951f0
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 37 deletions.
2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:debug": "nest start --debug --watch",
"start:local": "nest start --watch",
"start:local": "LOCAL=true nest start --watch",
"start:dev": "DEV=true nest start --watch",
"start:prod": "nest build && PROD=true node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
Expand Down
12 changes: 1 addition & 11 deletions app/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,7 @@ import { TeamInfoModule } from './page/teamInfo/teamInfo.module';
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: ((): string => {
if (process.env.PROD) {
return '../env/.env.prod';
}

if (process.env.DEV) {
return '../env/.env.dev';
}

return '../env/.env.local';
})(),
envFilePath: ['../env/.env.prod', '../env/.env.dev', '../env/.env.local'],
load: [
DATABASE_CONFIG,
FT_CLIENT_CONFIG,
Expand Down
30 changes: 18 additions & 12 deletions app/src/config/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,31 @@ import { registerAs } from '@nestjs/config';
import { findEnvByKey } from './util/findEnvByKey';

type DatabaseConfig = {
USERNAME: string;
PASSWORD: string;
ENDPOINT: string;
NAME: string;
CONNECTION_STRING: string;
DEV_CONNECTION_STRING: string;
LOCAL_CONNECTION_STRING: string;
};

export const DATABASE_CONFIG = registerAs('database', () => {
export const DATABASE_CONFIG_KEY = 'database';

export const DATABASE_CONFIG = registerAs(DATABASE_CONFIG_KEY, () => {
const NAME = findEnvByKey('DB_NAME');

const USERNAME = findEnvByKey('DB_USERNAME');
const PASSWORD = findEnvByKey('DB_PASSWORD');
const ENDPOINT = findEnvByKey('DB_ENDPOINT');
const NAME = findEnvByKey('DB_NAME');
const CONNECTION_STRING = `mongodb://${USERNAME}:${PASSWORD}@${ENDPOINT}/${NAME}`;

const DEV_USERNAME = findEnvByKey('DEV_DB_USERNAME');
const DEV_PASSWORD = findEnvByKey('DEV_DB_PASSWORD');
const DEV_ENDPOINT = findEnvByKey('DEV_DB_ENDPOINT');

const LOCAL_USERNAME = findEnvByKey('LOCAL_DB_USERNAME');
const LOCAL_PASSWORD = findEnvByKey('LOCAL_DB_PASSWORD');
const LOCAL_ENDPOINT = findEnvByKey('LOCAL_DB_ENDPOINT');

return {
USERNAME,
PASSWORD,
ENDPOINT,
NAME,
CONNECTION_STRING,
CONNECTION_STRING: `mongodb://${USERNAME}:${PASSWORD}@${ENDPOINT}/${NAME}`,
DEV_CONNECTION_STRING: `mongodb://${DEV_USERNAME}:${DEV_PASSWORD}@${DEV_ENDPOINT}/${NAME}`,
LOCAL_CONNECTION_STRING: `mongodb://${LOCAL_USERNAME}:${LOCAL_PASSWORD}@${LOCAL_ENDPOINT}/${NAME}`,
} satisfies DatabaseConfig;
});
24 changes: 22 additions & 2 deletions app/src/config/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import { findEnvByKey } from './util/findEnvByKey';
type RuntimeConfig = {
PROD: boolean;
DEV: boolean;
LOCAL: boolean;
TIMEZONE: string;
PORT: number;
};

export const RUNTIME_CONFIG = registerAs('runtime', () => {
export const RUNTIME_CONFIG_KEY = 'runtime';

export const RUNTIME_CONFIG = registerAs(RUNTIME_CONFIG_KEY, () => {
let PROD: boolean;

try {
Expand All @@ -17,14 +20,31 @@ export const RUNTIME_CONFIG = registerAs('runtime', () => {
PROD = false;
}

let DEV: boolean;

try {
DEV = findEnvByKey('DEV') === 'true';
} catch {
DEV = false;
}

let LOCAL: boolean;

try {
LOCAL = findEnvByKey('LOCAL') === 'true';
} catch {
LOCAL = false;
}

const PORT = parseInt(findEnvByKey('PORT'));
if (isNaN(PORT)) {
throw Error(`Wrong PORT value: ${PORT}`);
}

return {
PROD,
DEV: !PROD,
DEV,
LOCAL,
TIMEZONE: findEnvByKey('TZ'),
PORT,
} satisfies RuntimeConfig;
Expand Down
37 changes: 31 additions & 6 deletions app/src/database/mongoose/database.mongoose.module.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,40 @@
import { Module } from '@nestjs/common';
import type { ConfigType } from '@nestjs/config';
import { ConfigService, type ConfigType } from '@nestjs/config';
import { MongooseModule } from '@nestjs/mongoose';
import { DATABASE_CONFIG } from 'src/config/database';
import { type DATABASE_CONFIG, DATABASE_CONFIG_KEY } from 'src/config/database';
import { type RUNTIME_CONFIG, RUNTIME_CONFIG_KEY } from 'src/config/runtime';

@Module({
imports: [
MongooseModule.forRootAsync({
inject: [DATABASE_CONFIG.KEY],
useFactory: (databaseConfig: ConfigType<typeof DATABASE_CONFIG>) => ({
uri: databaseConfig.CONNECTION_STRING,
}),
inject: [ConfigService],
useFactory: (configService: ConfigService) => {
const databaseConfig =
configService.getOrThrow<ConfigType<typeof DATABASE_CONFIG>>(
DATABASE_CONFIG_KEY,
);

const runtimeConfig =
configService.getOrThrow<ConfigType<typeof RUNTIME_CONFIG>>(
RUNTIME_CONFIG_KEY,
);

let uri: string;

if (runtimeConfig.PROD) {
uri = databaseConfig.CONNECTION_STRING;
} else if (runtimeConfig.DEV) {
uri = databaseConfig.DEV_CONNECTION_STRING;
} else if (runtimeConfig.LOCAL) {
uri = databaseConfig.LOCAL_CONNECTION_STRING;
} else {
throw Error('Wrong env');
}

return {
uri,
};
},
}),
],
})
Expand Down
9 changes: 5 additions & 4 deletions app/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ import { ConfigService, ConfigType } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
import * as dotenv from 'dotenv';
import { AppModule } from './app.module';
import { RUNTIME_CONFIG } from './config/runtime';
import { type RUNTIME_CONFIG, RUNTIME_CONFIG_KEY } from './config/runtime';

async function bootstrap() {
dotenv.config();

const app = await NestFactory.create(AppModule);

const configService = app.get(ConfigService);
const runtimeConfig = configService.getOrThrow<
ConfigType<typeof RUNTIME_CONFIG>
>(RUNTIME_CONFIG.KEY);
const runtimeConfig =
configService.getOrThrow<ConfigType<typeof RUNTIME_CONFIG>>(
RUNTIME_CONFIG_KEY,
);

app.enableCors({
origin: runtimeConfig.PROD
Expand Down
2 changes: 1 addition & 1 deletion env
Submodule env updated from 4411c6 to 32a1ed

0 comments on commit 28951f0

Please sign in to comment.