Skip to content

Commit

Permalink
setSwagger 모듈화
Browse files Browse the repository at this point in the history
  • Loading branch information
masonJS committed Nov 2, 2023
1 parent cb8bde5 commit 04d4e34
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 65 deletions.
18 changes: 9 additions & 9 deletions src/libs/synchronizeEntities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ export const synchronizeEntities = async (): Promise<void> => {
}).compile();

const orm = module.get(MikroORM);
// const generator = orm.getSchemaGenerator();
// await generator.updateSchema({ safe: true });
const generator = orm.getSchemaGenerator();
await generator.updateSchema({ safe: true });

// const migrator = orm.getMigrator();
// try {
// await migrator.createMigration();
// await migrator.up();
// } catch (error) {
// await migrator.down();
// }
const migrator = orm.getMigrator();
try {
await migrator.createMigration();
await migrator.up();
} catch (error) {
await migrator.down();
}

await orm.close(true);

Expand Down
15 changes: 15 additions & 0 deletions src/libs/web-common/app/setSwagger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { INestApplication } from '@nestjs/common';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { SessionName } from '../../../constant/SessionConstant';

export function setSwagger(app: INestApplication) {
const config = new DocumentBuilder()
.setTitle('작심 API')
.setDescription('작심 API 문서')
.setVersion('1.0')
.addCookieAuth(SessionName)
.build();

const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api-docs', app, document);
}
68 changes: 16 additions & 52 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,27 @@
import 'reflect-metadata';
import {
BadRequestException,
ClassSerializerInterceptor,
ValidationPipe,
} from '@nestjs/common';
import { NestFactory, Reflector } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { ValidationError } from 'class-validator';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './AppModule';
import { BadParameterExceptionFilter } from './exceptions/BadParameterExceptionFilter';
import { HttpExceptionFilter } from './exceptions/HttpExceptionFilter';
import { CustomValidationError } from './exceptions/CustomValidationError';
import { SessionName } from './constant/SessionConstant';
import { Logger } from './libs/logger/Logger';
import { NotFoundExceptionFilter } from './exceptions/NotFoundExceptionFilter';
import { synchronizeEntities } from './libs/synchronizeEntities';
import { setNestApp } from './libs/web-common/app/setNestApp';
import { setCors } from './libs/web-common/app/setCors';
import { setSwagger } from './libs/web-common/app/setSwagger';

async function bootstrap() {
const app = await NestFactory.create(AppModule, {
logger: ['error', 'warn', 'debug', 'log', 'verbose'],
logger: process.env.NODE_ENV?.includes('local')
? ['error', 'warn', 'log']
: ['error', 'warn'],
});

try {
await synchronizeEntities();
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
}
// try {
// await synchronizeEntities();
// } catch (error) {
// eslint-disable-next-line no-console
// console.error(error);
// }

app.useGlobalFilters(
new NotFoundExceptionFilter(app.get(Logger)),
new BadParameterExceptionFilter(app.get(Logger)),
new HttpExceptionFilter(app.get(Logger)),
);
app.useGlobalInterceptors(new ClassSerializerInterceptor(app.get(Reflector)));

// 이렇게 해야 @Body()를 사용한 후 매개변수를 받으면 인스턴스화를 자동으로 진행한다.
// https://github.com/nestjs/nest/issues/552
app.useGlobalPipes(
new ValidationPipe({
transform: true,
validationError: {
value: true,
},
exceptionFactory: (validationErrors: ValidationError[] = []) =>
new BadRequestException(
validationErrors.map((error) => new CustomValidationError(error)),
),
}),
);

const config = new DocumentBuilder()
.setTitle('작심 API')
.setDescription('작심 API 문서')
.setVersion('1.0')
.addCookieAuth(SessionName)
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api-docs', app, document);
setNestApp(app);
setCors(process.env.NODE_ENV ?? 'local', app);
setSwagger(app);

const port = process.env.PORT || 3000;
await app.listen(port);
Expand Down
4 changes: 0 additions & 4 deletions src/module/session/SessionOptionModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,4 @@ export class SessionOptionModule {
genid: (_req: Request) => nanoid(this._sessionIdLength),
};
}

get sessionIdLength() {
return this._sessionIdLength;
}
}

0 comments on commit 04d4e34

Please sign in to comment.