Skip to content

Commit

Permalink
feat: host 스트림키 생성 api swagger 작성 / swagger 요청에 맞춰 body 데이터 DTO로 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
i3kae committed Nov 14, 2024
1 parent a1095b4 commit 04e6808
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
17 changes: 10 additions & 7 deletions backend/mainServer/src/host/host.controller.ts
Original file line number Diff line number Diff line change
@@ -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<string> })
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) {
Expand Down
12 changes: 9 additions & 3 deletions backend/mainServer/src/host/host.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -10,10 +16,10 @@ function generateRandomKey(uuid: string, salt: string) {

@Injectable()
export class HostService {
async generateStreamKey(uuid: string): Promise<Array<string>> {
async generateStreamKey(requestDto: keyGenerateRequestDto): Promise<Array<string>> {
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];
}
}

0 comments on commit 04e6808

Please sign in to comment.