Skip to content

Commit ac25dfd

Browse files
Make the java http server a gradle project (#26)
1 parent 54aeaab commit ac25dfd

File tree

5 files changed

+31
-14
lines changed

5 files changed

+31
-14
lines changed

examples/README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ This example uses `Ktor` to serve the front end.
3232

3333
## Java HTTP Server
3434

35-
This example uses the plain Java `HttpServer` to serve the front end. ([code](java-httpserver/server.kt))
35+
This example uses the plain Java `HttpServer` to serve the front end.
3636

37-
```shell
38-
cd ./java-httpserver ; jbang server.kt ; cd ..
39-
```
37+
- [Java HTTP Server](java-httpserver/src/main/kotlin/dev/datastar/kotlin/examples/httpserver/Application.kt): Demonstrates integration with Java HTTP Server.
4038

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
plugins {
2+
id("org.jetbrains.kotlin.jvm") version "2.2.0"
3+
application
4+
}
5+
6+
repositories {
7+
mavenCentral()
8+
}
9+
10+
application {
11+
mainClass = "dev.datastar.kotlin.examples.httpserver.ApplicationKt"
12+
}
13+
14+
dependencies {
15+
implementation("dev.data-star.kotlin:kotlin-sdk:1.0.0-RC1")
16+
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2")
17+
}
18+
19+
java {
20+
toolchain {
21+
languageVersion = JavaLanguageVersion.of(21)
22+
}
23+
}

examples/java-httpserver/server.kt renamed to examples/java-httpserver/src/main/kotlin/dev/datastar/kotlin/examples/httpserver/Application.kt

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
1+
package dev.datastar.kotlin.examples.httpserver
2+
13
import com.sun.net.httpserver.HttpExchange
24
import com.sun.net.httpserver.HttpServer
35
import dev.datastar.kotlin.sdk.Response
46
import dev.datastar.kotlin.sdk.ServerSentEventGenerator
57
import kotlinx.coroutines.flow.MutableStateFlow
68
import kotlinx.coroutines.runBlocking
7-
import java.io.File
89
import java.net.InetSocketAddress
910
import java.util.concurrent.Executors
1011

11-
///usr/bin/env jbang "$0" "$@" ; exit $?
12-
//JAVA 21
13-
//KOTLIN 2.2.0
14-
//DEPS dev.data-star.kotlin:kotlin-sdk:1.0.0-RC1
15-
//DEPS org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2
16-
1712

1813
fun main(): Unit = server().run {
1914
start()
@@ -29,9 +24,9 @@ fun server(
2924

3025
executor = Executors.newVirtualThreadPerTaskExecutor()
3126

32-
val counterPage = File(
33-
"../front/counter.html"
34-
).readBytes()
27+
val counterPage = object {}.javaClass.classLoader.getResource(
28+
"counter.html"
29+
)!!.readBytes()
3530

3631
createContext("/") { exchange ->
3732
exchange.responseHeaders.add("Content-Type", "text/html")

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ includeBuild("examples/spring/spring-webflux-example")
1111
includeBuild("examples/quarkus/quarkus-rest-example")
1212
includeBuild("examples/quarkus/quarkus-qute-example")
1313
includeBuild("examples/ktor/ktor-example")
14+
includeBuild("examples/java-httpserver")

0 commit comments

Comments
 (0)