Skip to content

Commit 507bc09

Browse files
committed
Swagger 설정에 Basic Auth 추가 및 활성화 환경변수 지원
1 parent 344220a commit 507bc09

File tree

4 files changed

+56
-10
lines changed

4 files changed

+56
-10
lines changed

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
DATABASE_URL=postgres://marrylife:marrylife@db:5432/marrylife_db
22
PORT=4000
3+
4+
# Swagger 설정
5+
SWAGGER_ENABLED=true
6+
SWAGGER_USER=admin
7+
SWAGGER_PASSWORD=adminpw

apps/api/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"class-transformer": "^0.5.1",
3535
"class-validator": "^0.14.4",
3636
"cookie-parser": "^1.4.7",
37+
"express-basic-auth": "^1.2.1",
3738
"reflect-metadata": "^0.2.2",
3839
"rxjs": "^7.8.1",
3940
"swagger-ui-express": "^5.0.1"

apps/api/src/main.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { NestFactory } from '@nestjs/core';
33
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
44
import { runMigrations } from '@marrylife/db';
55
import cookieParser from 'cookie-parser';
6+
import basicAuth from 'express-basic-auth';
67
import { AppModule } from './app.module';
78
import { HttpExceptionFilter } from './common/filters/http-exception.filter';
89
import { ResponseInterceptor } from './common/interceptors/response.interceptor';
@@ -31,16 +32,32 @@ async function bootstrap() {
3132
app.useGlobalInterceptors(new ResponseInterceptor());
3233
app.useGlobalFilters(new HttpExceptionFilter());
3334

34-
const swaggerConfig = new DocumentBuilder()
35-
.setTitle('MarryLife API')
36-
.setDescription('MarryLife API 문서')
37-
.setVersion('1.0.0')
38-
.addBearerAuth()
39-
.build();
40-
const swaggerDocument = SwaggerModule.createDocument(app, swaggerConfig);
41-
SwaggerModule.setup('docs', app, swaggerDocument, {
42-
useGlobalPrefix: true,
43-
});
35+
const swaggerEnabled = process.env.SWAGGER_ENABLED !== 'false';
36+
if (swaggerEnabled) {
37+
const swaggerUser = process.env.SWAGGER_USER;
38+
const swaggerPassword = process.env.SWAGGER_PASSWORD;
39+
if (swaggerUser && swaggerPassword) {
40+
app.use(
41+
'/api/docs',
42+
basicAuth({
43+
users: { [swaggerUser]: swaggerPassword },
44+
challenge: true,
45+
realm: 'MarryLife API Docs',
46+
}),
47+
);
48+
}
49+
50+
const swaggerConfig = new DocumentBuilder()
51+
.setTitle('MarryLife API')
52+
.setDescription('MarryLife API 문서')
53+
.setVersion('1.0.0')
54+
.addBearerAuth()
55+
.build();
56+
const swaggerDocument = SwaggerModule.createDocument(app, swaggerConfig);
57+
SwaggerModule.setup('docs', app, swaggerDocument, {
58+
useGlobalPrefix: true,
59+
});
60+
}
4461

4562
const port = process.env.PORT ?? 4000;
4663
await app.listen(port);

pnpm-lock.yaml

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)