Skip to content

Commit 04e6808

Browse files
committed
feat: host 스트림키 생성 api swagger 작성 / swagger 요청에 맞춰 body 데이터 DTO로 작성
1 parent a1095b4 commit 04e6808

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

backend/mainServer/src/host/host.controller.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
1-
import { Controller, Post, Req, Res, HttpException, HttpStatus } from '@nestjs/common';
2-
import { HostService } from './host.service.js';
1+
import { Controller, Post, Req, Res, HttpException, HttpStatus, Body } from '@nestjs/common';
2+
import { keyGenerateRequestDto, HostService } from './host.service.js';
33
import { Request, Response } from 'express';
4+
import { ApiCreatedResponse, ApiOperation, ApiTags } from '@nestjs/swagger';
45

56
@Controller('host')
7+
@ApiTags('Host API')
68
export class HostController {
79
constructor(private readonly hostService: HostService) {}
810

9-
@Post('/')
10-
async generateStreamKey(@Req() req: Request, @Res() res: Response) {
11+
@Post('/key')
12+
@ApiOperation({ summary: 'Host Stream, Session Key Generate API', description: 'Host용 스트림키와 세션키를 생성합니다.' })
13+
@ApiCreatedResponse({ description: '스트림키, 세션키를 생성한다.', type: Array<string> })
14+
async generateStreamKey(@Body() requestDto: keyGenerateRequestDto, @Req() req: Request, @Res() res: Response) {
1115
try {
1216
const host = req.headers['host'] as string;
1317
const contentType = req.headers['content-type'];
14-
const { uuid } = req.body;
1518

16-
if (!host || !contentType || !uuid) {
19+
if (!host || !contentType || !requestDto.uuid) {
1720
throw new HttpException('Bad Request', HttpStatus.BAD_REQUEST);
1821
}
1922
if (contentType !== 'application/json') {
2023
throw new HttpException('Content-Type must be application/json', HttpStatus.BAD_REQUEST);
2124
}
2225

23-
const [streamKey, sessionKey] = await this.hostService.generateStreamKey(uuid);
26+
const [streamKey, sessionKey] = await this.hostService.generateStreamKey(requestDto);
2427
res.status(HttpStatus.OK).json({ 'stream-key': streamKey, 'session-key':sessionKey });
2528
} catch (error) {
2629
if ((error as { status: number }).status === 400) {

backend/mainServer/src/host/host.service.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import { Injectable } from '@nestjs/common';
22
import dotenv from 'dotenv';
33
import path from 'path';
44
import crypto from 'crypto';
5+
import { ApiProperty } from '@nestjs/swagger';
6+
7+
export class keyGenerateRequestDto {
8+
@ApiProperty({example: 'this_is_uuid', description: '클라이언트마다 가지는 랜덤한 uuid 값'})
9+
uuid: string = '';
10+
}
511

612
function generateRandomKey(uuid: string, salt: string) {
713
const hash = crypto.createHmac('sha256', salt).update(uuid).digest('hex');
@@ -10,10 +16,10 @@ function generateRandomKey(uuid: string, salt: string) {
1016

1117
@Injectable()
1218
export class HostService {
13-
async generateStreamKey(uuid: string): Promise<Array<string>> {
19+
async generateStreamKey(requestDto: keyGenerateRequestDto): Promise<Array<string>> {
1420
dotenv.config({path: path.resolve('../.env')});
15-
const streamKey = generateRandomKey(uuid, process.env.STREAM_KEY_SALT || '');
16-
const sessionKey = generateRandomKey(uuid, process.env.SESSION_KEY_SALT || '');
21+
const streamKey = generateRandomKey(requestDto.uuid, process.env.STREAM_KEY_SALT || '');
22+
const sessionKey = generateRandomKey(requestDto.uuid, process.env.SESSION_KEY_SALT || '');
1723
return [streamKey, sessionKey];
1824
}
1925
}

0 commit comments

Comments
 (0)