Skip to content

Commit

Permalink
fix: 프로덕션 환경에서는 특정 Origin만 참조하도록 수정 (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
Coalery authored Nov 17, 2024
2 parents 8593bc7 + 4615312 commit 5a0ca51
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/core/config/AppConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export type AppConfig = {
env: 'development' | 'production';
};

const validateEnv = (env: string = 'development'): AppConfig['env'] => {
if (env !== 'development' && env !== 'production') {
throw new Error(`Invalid NODE_ENV: ${env}`);
}
return env as AppConfig['env'];
};

export const config = (): AppConfig => ({
env: validateEnv(process.env.NODE_ENV),
});
3 changes: 3 additions & 0 deletions src/core/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import * as app from '@khlug/core/config/AppConfig';
import * as db from '@khlug/core/config/DatabaseConfig';
import * as session from '@khlug/core/config/LaravelSessionConfig';

export const configuration = (): {
app: app.AppConfig;
database: db.DatabaseConfig;
session: session.LaravelSessionConfig;
} => ({
app: app.config(),
database: db.config(),
session: session.config(),
});
12 changes: 11 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import { CorsOptions } from '@nestjs/common/interfaces/external/cors-options.interface';
import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
import cookieParser from 'cookie-parser';

import { AppConfig } from '@khlug/core/config/AppConfig';

import { RootModule } from '@khlug/RootModule';

async function bootstrap() {
const app = await NestFactory.create(RootModule);

const appConfig = app.get(ConfigService).getOrThrow<AppConfig>('app');
const corsOptions: CorsOptions | undefined =
appConfig.env === 'production'
? { origin: ['https://khlug.org', 'https://app.khlug.org'] }
: undefined;

app.enableCors(corsOptions);
app.use(cookieParser());
app.enableCors();

await app.listen(3000);
}
Expand Down

0 comments on commit 5a0ca51

Please sign in to comment.