Skip to content

Commit

Permalink
Feat: proxy endpoint 추가
Browse files Browse the repository at this point in the history
- kakao map image 조회를 위해 proxy endpoint 추가
  • Loading branch information
sally0226 committed Aug 17, 2024
1 parent 5ccf986 commit 5b6b971
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { envValidation } from './common/helper/env.validation';
import { GptModule } from './gpt/gpt.module';
import { MapModule } from './map/map.module';
import { PlaceModule } from './place/place.module';
import { ProxyModule } from './proxy/proxy.module';
import { SearchModule } from './search/search.module';
import { UserModule } from './user/user.module';
import { UtilModule } from './util/util.module';
Expand All @@ -34,6 +35,7 @@ import { UtilModule } from './util/util.module';
UtilModule,
PlaceModule,
InviteLinkModule,
ProxyModule,
],
controllers: [],
providers: [],
Expand Down
21 changes: 21 additions & 0 deletions src/proxy/proxy.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Controller, Get, Query, Response } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';

import { Response as Res } from 'express';

import { ProxyService } from 'src/proxy/proxy.service';

@ApiTags('proxy')
@Controller('proxy')
export class ProxyController {
constructor(private readonly proxyService: ProxyService) {}

// TODO : 한정된 도메인에 대해서만 proxy 하도록 조건 추가 필요
@Get()
async getKakaoImage(@Query('url') url: string, @Response() res: Res) {
const result = await this.proxyService.fetchKakaoImage(url);

res.setHeader('Content-Type', result.contentType);
res.send(result.data);
}
}
12 changes: 12 additions & 0 deletions src/proxy/proxy.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { HttpModule } from '@nestjs/axios';
import { Module } from '@nestjs/common';

import { ProxyController } from './proxy.controller';
import { ProxyService } from './proxy.service';

@Module({
imports: [HttpModule],
controllers: [ProxyController],
providers: [ProxyService],
})
export class ProxyModule {}
22 changes: 22 additions & 0 deletions src/proxy/proxy.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { HttpService } from '@nestjs/axios';
import { Injectable } from '@nestjs/common';

import { map } from 'rxjs/operators';

@Injectable()
export class ProxyService {
constructor(private readonly httpService: HttpService) {}

// 카카오 API로부터 이미지를 가져오는 메서드
async fetchKakaoImage(url: string): Promise<any> {
return this.httpService
.get(url, { responseType: 'arraybuffer' })
.pipe(
map((response) => ({
data: response.data,
contentType: response.headers['content-type'],
})),
)
.toPromise();
}
}

0 comments on commit 5b6b971

Please sign in to comment.