refactor: simplify WebSocketConfig with Annotation

This commit is contained in:
SOR
2026-07-11 11:22:20 +08:00
parent 3df38b5757
commit 629ff6a3c7
5 changed files with 28 additions and 15 deletions
+12
View File
@@ -0,0 +1,12 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PyShadowingBuiltinsInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<option name="ignoredNames">
<list>
<option value="id" />
</list>
</option>
</inspection_tool>
</profile>
</component>
@@ -1,29 +1,24 @@
package com.github.sor2171.backend.config package com.github.sor2171.backend.config
import com.github.sor2171.backend.handler.TestHandler import com.github.sor2171.backend.handler.WsHandler
import com.github.sor2171.backend.handler.v1.ExampleHandler
import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Configuration
import org.springframework.web.reactive.HandlerMapping import org.springframework.web.reactive.HandlerMapping
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping
import org.springframework.web.reactive.socket.WebSocketHandler
import org.springframework.web.reactive.socket.server.support.WebSocketHandlerAdapter import org.springframework.web.reactive.socket.server.support.WebSocketHandlerAdapter
import kotlin.reflect.full.findAnnotation
@Configuration @Configuration
class WebSocketConfig( class WebSocketConfig(
private val exampleHandler: ExampleHandler, private val handlers: List<WebSocketHandler>
private val testHandler: TestHandler
) { ) {
@Bean @Bean
fun handlerMapping(): HandlerMapping { fun handlerMapping(): HandlerMapping {
val map = mapOf( val map = handlers.associateBy {
"/ws/v1/example" to exampleHandler, it::class.findAnnotation<WsHandler>()!!.path
"/ws/test" to testHandler }
) return SimpleUrlHandlerMapping(map, 1)
val mapping = SimpleUrlHandlerMapping()
mapping.urlMap = map
mapping.order = 1
return mapping
} }
@Bean @Bean
@@ -6,9 +6,9 @@ import org.springframework.web.reactive.socket.WebSocketSession
import reactor.core.publisher.Mono import reactor.core.publisher.Mono
@Component @Component
@WsHandler("/ws/test")
class TestHandler : WebSocketHandler { class TestHandler : WebSocketHandler {
override fun handle(session: WebSocketSession): Mono<Void> { override fun handle(session: WebSocketSession): Mono<Void> {
// ws://localhost:8080/ws/test
val output = session.receive() val output = session.receive()
.map { msg -> .map { msg ->
val userText = msg.payloadAsText val userText = msg.payloadAsText
@@ -0,0 +1,5 @@
package com.github.sor2171.backend.handler
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
annotation class WsHandler(val path: String)
@@ -1,5 +1,6 @@
package com.github.sor2171.backend.handler.v1 package com.github.sor2171.backend.handler.v1
import com.github.sor2171.backend.handler.WsHandler
import com.github.sor2171.backend.service.WsTokenService import com.github.sor2171.backend.service.WsTokenService
import org.springframework.stereotype.Component import org.springframework.stereotype.Component
import org.springframework.web.reactive.socket.WebSocketHandler import org.springframework.web.reactive.socket.WebSocketHandler
@@ -7,11 +8,11 @@ import org.springframework.web.reactive.socket.WebSocketSession
import reactor.core.publisher.Mono import reactor.core.publisher.Mono
@Component @Component
@WsHandler("/ws/v1/example")
class ExampleHandler( class ExampleHandler(
private val wsTokenService: WsTokenService, private val wsTokenService: WsTokenService,
) : WebSocketHandler { ) : WebSocketHandler {
override fun handle(session: WebSocketSession): Mono<Void> { override fun handle(session: WebSocketSession): Mono<Void> {
// ws://localhost:8080/ws/v1/example?token=
val output = session.receive() val output = session.receive()
.map { msg -> .map { msg ->
val userText = msg.payloadAsText val userText = msg.payloadAsText