From 80513c0146e62420e9bac2c60a94d4c37f9ced2d Mon Sep 17 00:00:00 2001 From: jungmin Date: Mon, 15 Jul 2024 13:14:21 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=B4=88=EA=B8=B0=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/build.gradle | 35 ++++------ .../edufocus/video/controller/Controller.java | 64 +++++++++++++++++++ .../src/main/resources/application.properties | 16 +++++ 3 files changed, 94 insertions(+), 21 deletions(-) create mode 100644 backend/src/main/java/com/edufocus/edufocus/video/controller/Controller.java diff --git a/backend/build.gradle b/backend/build.gradle index d1068ef..6c0680f 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -1,38 +1,31 @@ plugins { id 'java' - id 'org.springframework.boot' version '3.3.1' - id 'io.spring.dependency-management' version '1.1.5' + id 'org.springframework.boot' version '3.2.7' + id 'io.spring.dependency-management' version '1.1.0' } -group = 'com.edufocus' +group = 'io.openvidu' version = '0.0.1-SNAPSHOT' - -java { - toolchain { - languageVersion = JavaLanguageVersion.of(17) - } -} - -configurations { - compileOnly { - extendsFrom annotationProcessor - } -} +description = 'Basic server application built for Java with Spring Boot' +sourceCompatibility = '17' +targetCompatibility = '17' repositories { mavenCentral() } dependencies { - implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-web' - compileOnly 'org.projectlombok:lombok' - runtimeOnly 'com.mysql:mysql-connector-j' - annotationProcessor 'org.projectlombok:lombok' + implementation 'org.openpnp:opencv:4.9.0-0' + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + implementation 'io.livekit:livekit-server:0.5.11' + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + implementation 'mysql:mysql-connector-java:8.0.33' testImplementation 'org.springframework.boot:spring-boot-starter-test' - testRuntimeOnly 'org.junit.platform:junit-platform-launcher' + implementation group: 'io.livekit', name: 'livekit-server', version: '0.6.1' + } -tasks.named('test') { +test { useJUnitPlatform() } diff --git a/backend/src/main/java/com/edufocus/edufocus/video/controller/Controller.java b/backend/src/main/java/com/edufocus/edufocus/video/controller/Controller.java new file mode 100644 index 0000000..ab3a751 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/video/controller/Controller.java @@ -0,0 +1,64 @@ +package com.edufocus.edufocus.video.controller; + +import io.livekit.server.AccessToken; +import io.livekit.server.RoomJoin; +import io.livekit.server.RoomName; +import io.livekit.server.WebhookReceiver; +import livekit.LivekitWebhook.WebhookEvent; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.Map; + +@CrossOrigin(origins = "*") +@RestController +public class Controller { + + @Value("${livekit.api.key}") + private String LIVEKIT_API_KEY; + + @Value("${livekit.api.secret}") + private String LIVEKIT_API_SECRET; + + /** + * @param params JSON object with roomName and participantName + * @return JSON object with the JWT token + */ + @PostMapping(value = "/token") + public ResponseEntity> createToken(@RequestBody Map params) { + String roomName = params.get("roomName"); + String participantName = params.get("participantName"); + + if (roomName == null || participantName == null) { + return ResponseEntity.badRequest().body(Map.of("errorMessage", "roomName and participantName are required")); + } + + AccessToken token = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET); + + + token.setName(participantName); + token.setIdentity(participantName); + token.addGrants(new RoomJoin(true), new RoomName(roomName)); + + // 방이름으로 입장하는건데 + // 만약 수강생에 DB에 신청한 강의명이 없다면 들어갈수 없음 + // 검증 로직만 추가하면 될듯? + + + return ResponseEntity.ok(Map.of("token", token.toJwt())); + } + + @PostMapping(value = "/webhook", consumes = "application/webhook+json") + public ResponseEntity receiveWebhook(@RequestHeader("Authorization") String authHeader, @RequestBody String body) { + WebhookReceiver webhookReceiver = new WebhookReceiver(LIVEKIT_API_KEY, LIVEKIT_API_SECRET); + try { + WebhookEvent event = webhookReceiver.receive(body, authHeader); + System.out.println("LiveKit Webhook: " + event.toString()); + } catch (Exception e) { + System.err.println("Error validating webhook event: " + e.getMessage()); + } + return ResponseEntity.ok("ok"); + } + +} diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index d5ad940..172132c 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -1 +1,17 @@ spring.application.name=edufocus + +server.port=${SERVER_PORT:6080} +server.ssl.enabled=false + +# LiveKit configuration +livekit.api.key=${LIVEKIT_API_KEY:devkey} +livekit.api.secret=${LIVEKIT_API_SECRET:secret} + +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver +spring.datasource.url=jdbc:mysql://localhost:3306/edufocus?serverTimezone=Asia/Seoul +spring.datasource.username=root +spring.datasource.password=root + +spring.jpa.database=mysql +spring.jpa.hibernate.ddl-auto=update +spring.jpa.show-sql=true