diff --git a/src/main/kotlin/com/cubetiqs/wsserver/WsServerApplication.kt b/src/main/kotlin/com/cubetiqs/wsserver/WsServerApplication.kt new file mode 100644 index 0000000..93dd77f --- /dev/null +++ b/src/main/kotlin/com/cubetiqs/wsserver/WsServerApplication.kt @@ -0,0 +1,63 @@ +package com.cubetiqs.wsserver + +import org.springframework.boot.autoconfigure.SpringBootApplication +import org.springframework.boot.runApplication +import org.springframework.http.MediaType +import org.springframework.messaging.handler.annotation.MessageMapping +import org.springframework.messaging.rsocket.RSocketRequester +import org.springframework.stereotype.Controller +import org.springframework.stereotype.Service +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.RestController +import reactor.core.publisher.Flux +import java.time.Duration +import java.time.LocalDateTime +import java.util.concurrent.ThreadLocalRandom + +@SpringBootApplication +class WsServerApplication + +fun main(args: Array) { + runApplication(*args) +} + +@RestController +class RestController (val priceService: PriceService) { + @GetMapping( + value = ["/stocks/{symbol}"], + produces = [MediaType.TEXT_EVENT_STREAM_VALUE] + ) + fun prices( + @PathVariable symbol: String + ): Flux { + return priceService.generate(symbol) + } +} + +@Controller +class RSocketController(val priceService: PriceService) { + @MessageMapping("stockPrices") + fun prices(symbol: String): Flux { + return priceService.generate(symbol) + } +} + +@Service +class PriceService { + fun generate(symbol: String): Flux { + return Flux + .interval(Duration.ofSeconds(1)) + .map { StockPrice(symbol, randomStockPrice(), LocalDateTime.now()) } + } + + private fun randomStockPrice(): Double { + return ThreadLocalRandom.current().nextDouble(100.0) + } +} + +data class StockPrice ( + val symbol: String, + val price: Double, + val time: LocalDateTime +) \ No newline at end of file diff --git a/src/main/kotlin/com/cubetiqs/wsserver/WsserverApplication.kt b/src/main/kotlin/com/cubetiqs/wsserver/WsserverApplication.kt deleted file mode 100644 index 4df3240..0000000 --- a/src/main/kotlin/com/cubetiqs/wsserver/WsserverApplication.kt +++ /dev/null @@ -1,11 +0,0 @@ -package com.cubetiqs.wsserver - -import org.springframework.boot.autoconfigure.SpringBootApplication -import org.springframework.boot.runApplication - -@SpringBootApplication -class WsserverApplication - -fun main(args: Array) { - runApplication(*args) -} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b13789..cab786c 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1 @@ - +spring.rsocket.server.port=7000 \ No newline at end of file diff --git a/src/test/kotlin/com/cubetiqs/wsserver/WsserverApplicationTests.kt b/src/test/kotlin/com/cubetiqs/wsserver/WsServerApplicationTests.kt similarity index 84% rename from src/test/kotlin/com/cubetiqs/wsserver/WsserverApplicationTests.kt rename to src/test/kotlin/com/cubetiqs/wsserver/WsServerApplicationTests.kt index dfa72a4..72ca4e1 100644 --- a/src/test/kotlin/com/cubetiqs/wsserver/WsserverApplicationTests.kt +++ b/src/test/kotlin/com/cubetiqs/wsserver/WsServerApplicationTests.kt @@ -4,7 +4,7 @@ import org.junit.jupiter.api.Test import org.springframework.boot.test.context.SpringBootTest @SpringBootTest -class WsserverApplicationTests { +class WsServerApplicationTests { @Test fun contextLoads() {