|
| 1 | +import dev.datastar.kotlin.sdk.Response |
| 2 | +import dev.datastar.kotlin.sdk.ServerSentEventGenerator |
| 3 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 4 | +import kotlinx.coroutines.runBlocking |
| 5 | +import org.springframework.boot.SpringApplication |
| 6 | +import org.springframework.boot.SpringBootConfiguration |
| 7 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration |
| 8 | +import org.springframework.context.annotation.Import |
| 9 | +import org.springframework.http.HttpStatus |
| 10 | +import org.springframework.http.MediaType |
| 11 | +import org.springframework.web.bind.annotation.GetMapping |
| 12 | +import org.springframework.web.bind.annotation.PostMapping |
| 13 | +import org.springframework.web.bind.annotation.ResponseStatus |
| 14 | +import org.springframework.web.bind.annotation.RestController |
| 15 | +import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody |
| 16 | +import java.io.File |
| 17 | +import java.io.OutputStream |
| 18 | +import java.io.OutputStreamWriter |
| 19 | + |
| 20 | +///usr/bin/env jbang "$0" "$@" ; exit $? |
| 21 | +//JAVA 21 |
| 22 | +//KOTLIN 2.2.0 |
| 23 | +//DEPS dev.data-star.kotlin:kotlin-sdk:1.0.0-RC1 |
| 24 | +//DEPS org.springframework.boot:spring-boot-starter-web:3.5.5 |
| 25 | +//DEPS org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2 |
| 26 | + |
| 27 | +fun main() { |
| 28 | + SpringApplication.run(CounterApp::class.java) |
| 29 | +} |
| 30 | + |
| 31 | +@SpringBootConfiguration |
| 32 | +@EnableAutoConfiguration |
| 33 | +@Import(CounterController::class) |
| 34 | +open class CounterApp |
| 35 | + |
| 36 | +@RestController |
| 37 | +class CounterController { |
| 38 | + |
| 39 | + private val counter = MutableStateFlow(0) |
| 40 | + |
| 41 | + @GetMapping("/", produces = [MediaType.TEXT_HTML_VALUE]) |
| 42 | + @ResponseStatus(HttpStatus.OK) |
| 43 | + fun index() = StreamingResponseBody { stream -> |
| 44 | + File("../front/counter.html").inputStream().use { inputStream -> |
| 45 | + inputStream.transferTo(stream) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @GetMapping("/counter", produces = [MediaType.TEXT_EVENT_STREAM_VALUE]) |
| 50 | + @ResponseStatus(HttpStatus.OK) |
| 51 | + fun counter() = StreamingResponseBody { stream -> |
| 52 | + runBlocking { |
| 53 | + val response = adapterResponse(stream) |
| 54 | + val generator = ServerSentEventGenerator(response) |
| 55 | + counter.collect { event -> |
| 56 | + generator.patchElements("""<span id="counter">${event}</span>""") |
| 57 | + |
| 58 | + if (event == 3) { |
| 59 | + generator.executeScript("""alert('Thanks for trying Datastar!')""") |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + @PostMapping("/increment") |
| 67 | + @ResponseStatus(HttpStatus.NO_CONTENT) |
| 68 | + fun increment() { |
| 69 | + counter.value++ |
| 70 | + } |
| 71 | + |
| 72 | + @PostMapping("/decrement") |
| 73 | + @ResponseStatus(HttpStatus.NO_CONTENT) |
| 74 | + fun decrement() { |
| 75 | + counter.value-- |
| 76 | + } |
| 77 | + |
| 78 | + private fun adapterResponse(stream: OutputStream): Response = object : Response { |
| 79 | + private val writer = OutputStreamWriter(stream) |
| 80 | + |
| 81 | + override fun sendConnectionHeaders( |
| 82 | + status: Int, |
| 83 | + headers: Map<String, List<String>> |
| 84 | + ) = Unit |
| 85 | + |
| 86 | + override fun write(text: String) = writer.write(text) |
| 87 | + |
| 88 | + override fun flush() = writer.flush() |
| 89 | + |
| 90 | + } |
| 91 | +} |
0 commit comments