From 04e6808b5b8fa5549b905a6a1a88333ff4e4255a Mon Sep 17 00:00:00 2001 From: i3kae Date: Thu, 14 Nov 2024 14:52:02 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20host=20=EC=8A=A4=ED=8A=B8=EB=A6=BC?= =?UTF-8?q?=ED=82=A4=20=EC=83=9D=EC=84=B1=20api=20swagger=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1=20/=20swagger=20=EC=9A=94=EC=B2=AD=EC=97=90=20?= =?UTF-8?q?=EB=A7=9E=EC=B6=B0=20body=20=EB=8D=B0=EC=9D=B4=ED=84=B0=20DTO?= =?UTF-8?q?=EB=A1=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/mainServer/src/host/host.controller.ts | 17 ++++++++++------- backend/mainServer/src/host/host.service.ts | 12 +++++++++--- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/backend/mainServer/src/host/host.controller.ts b/backend/mainServer/src/host/host.controller.ts index 5999d728..04c11e1e 100644 --- a/backend/mainServer/src/host/host.controller.ts +++ b/backend/mainServer/src/host/host.controller.ts @@ -1,26 +1,29 @@ -import { Controller, Post, Req, Res, HttpException, HttpStatus } from '@nestjs/common'; -import { HostService } from './host.service.js'; +import { Controller, Post, Req, Res, HttpException, HttpStatus, Body } from '@nestjs/common'; +import { keyGenerateRequestDto, HostService } from './host.service.js'; import { Request, Response } from 'express'; +import { ApiCreatedResponse, ApiOperation, ApiTags } from '@nestjs/swagger'; @Controller('host') +@ApiTags('Host API') export class HostController { constructor(private readonly hostService: HostService) {} - @Post('/') - async generateStreamKey(@Req() req: Request, @Res() res: Response) { + @Post('/key') + @ApiOperation({ summary: 'Host Stream, Session Key Generate API', description: 'Host용 스트림키와 세션키를 생성합니다.' }) + @ApiCreatedResponse({ description: '스트림키, 세션키를 생성한다.', type: Array }) + async generateStreamKey(@Body() requestDto: keyGenerateRequestDto, @Req() req: Request, @Res() res: Response) { try { const host = req.headers['host'] as string; const contentType = req.headers['content-type']; - const { uuid } = req.body; - if (!host || !contentType || !uuid) { + if (!host || !contentType || !requestDto.uuid) { throw new HttpException('Bad Request', HttpStatus.BAD_REQUEST); } if (contentType !== 'application/json') { throw new HttpException('Content-Type must be application/json', HttpStatus.BAD_REQUEST); } - const [streamKey, sessionKey] = await this.hostService.generateStreamKey(uuid); + const [streamKey, sessionKey] = await this.hostService.generateStreamKey(requestDto); res.status(HttpStatus.OK).json({ 'stream-key': streamKey, 'session-key':sessionKey }); } catch (error) { if ((error as { status: number }).status === 400) { diff --git a/backend/mainServer/src/host/host.service.ts b/backend/mainServer/src/host/host.service.ts index 891dc8b6..645f437a 100644 --- a/backend/mainServer/src/host/host.service.ts +++ b/backend/mainServer/src/host/host.service.ts @@ -2,6 +2,12 @@ import { Injectable } from '@nestjs/common'; import dotenv from 'dotenv'; import path from 'path'; import crypto from 'crypto'; +import { ApiProperty } from '@nestjs/swagger'; + +export class keyGenerateRequestDto { + @ApiProperty({example: 'this_is_uuid', description: '클라이언트마다 가지는 랜덤한 uuid 값'}) + uuid: string = ''; +} function generateRandomKey(uuid: string, salt: string) { const hash = crypto.createHmac('sha256', salt).update(uuid).digest('hex'); @@ -10,10 +16,10 @@ function generateRandomKey(uuid: string, salt: string) { @Injectable() export class HostService { - async generateStreamKey(uuid: string): Promise> { + async generateStreamKey(requestDto: keyGenerateRequestDto): Promise> { dotenv.config({path: path.resolve('../.env')}); - const streamKey = generateRandomKey(uuid, process.env.STREAM_KEY_SALT || ''); - const sessionKey = generateRandomKey(uuid, process.env.SESSION_KEY_SALT || ''); + const streamKey = generateRandomKey(requestDto.uuid, process.env.STREAM_KEY_SALT || ''); + const sessionKey = generateRandomKey(requestDto.uuid, process.env.SESSION_KEY_SALT || ''); return [streamKey, sessionKey]; } } \ No newline at end of file