diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..02d4983 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,12 @@ + + + + \ No newline at end of file diff --git a/backend/src/main/kotlin/com/github/sor2171/backend/config/WebSocketConfig.kt b/backend/src/main/kotlin/com/github/sor2171/backend/config/WebSocketConfig.kt index 0c4061e..ed75510 100644 --- a/backend/src/main/kotlin/com/github/sor2171/backend/config/WebSocketConfig.kt +++ b/backend/src/main/kotlin/com/github/sor2171/backend/config/WebSocketConfig.kt @@ -1,29 +1,24 @@ package com.github.sor2171.backend.config -import com.github.sor2171.backend.handler.TestHandler -import com.github.sor2171.backend.handler.v1.ExampleHandler +import com.github.sor2171.backend.handler.WsHandler import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.web.reactive.HandlerMapping import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping +import org.springframework.web.reactive.socket.WebSocketHandler import org.springframework.web.reactive.socket.server.support.WebSocketHandlerAdapter +import kotlin.reflect.full.findAnnotation @Configuration class WebSocketConfig( - private val exampleHandler: ExampleHandler, - private val testHandler: TestHandler + private val handlers: List ) { - @Bean fun handlerMapping(): HandlerMapping { - val map = mapOf( - "/ws/v1/example" to exampleHandler, - "/ws/test" to testHandler - ) - val mapping = SimpleUrlHandlerMapping() - mapping.urlMap = map - mapping.order = 1 - return mapping + val map = handlers.associateBy { + it::class.findAnnotation()!!.path + } + return SimpleUrlHandlerMapping(map, 1) } @Bean diff --git a/backend/src/main/kotlin/com/github/sor2171/backend/handler/TestHandler.kt b/backend/src/main/kotlin/com/github/sor2171/backend/handler/TestHandler.kt index 89f9c84..5e0fa51 100644 --- a/backend/src/main/kotlin/com/github/sor2171/backend/handler/TestHandler.kt +++ b/backend/src/main/kotlin/com/github/sor2171/backend/handler/TestHandler.kt @@ -6,9 +6,9 @@ import org.springframework.web.reactive.socket.WebSocketSession import reactor.core.publisher.Mono @Component +@WsHandler("/ws/test") class TestHandler : WebSocketHandler { override fun handle(session: WebSocketSession): Mono { - // ws://localhost:8080/ws/test val output = session.receive() .map { msg -> val userText = msg.payloadAsText diff --git a/backend/src/main/kotlin/com/github/sor2171/backend/handler/WsHandler.kt b/backend/src/main/kotlin/com/github/sor2171/backend/handler/WsHandler.kt new file mode 100644 index 0000000..8a63012 --- /dev/null +++ b/backend/src/main/kotlin/com/github/sor2171/backend/handler/WsHandler.kt @@ -0,0 +1,5 @@ +package com.github.sor2171.backend.handler + +@Target(AnnotationTarget.CLASS) +@Retention(AnnotationRetention.RUNTIME) +annotation class WsHandler(val path: String) diff --git a/backend/src/main/kotlin/com/github/sor2171/backend/handler/v1/ExampleHandler.kt b/backend/src/main/kotlin/com/github/sor2171/backend/handler/v1/ExampleHandler.kt index e709ba4..31955f6 100644 --- a/backend/src/main/kotlin/com/github/sor2171/backend/handler/v1/ExampleHandler.kt +++ b/backend/src/main/kotlin/com/github/sor2171/backend/handler/v1/ExampleHandler.kt @@ -1,5 +1,6 @@ package com.github.sor2171.backend.handler.v1 +import com.github.sor2171.backend.handler.WsHandler import com.github.sor2171.backend.service.WsTokenService import org.springframework.stereotype.Component import org.springframework.web.reactive.socket.WebSocketHandler @@ -7,11 +8,11 @@ import org.springframework.web.reactive.socket.WebSocketSession import reactor.core.publisher.Mono @Component +@WsHandler("/ws/v1/example") class ExampleHandler( private val wsTokenService: WsTokenService, ) : WebSocketHandler { override fun handle(session: WebSocketSession): Mono { - // ws://localhost:8080/ws/v1/example?token= val output = session.receive() .map { msg -> val userText = msg.payloadAsText