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