Skip to content

Commit cf0f525

Browse files
Update README and license of published artifact to point to Datastar version
1 parent cc63bc9 commit cf0f525

File tree

4 files changed

+68
-74
lines changed

4 files changed

+68
-74
lines changed

README.md

Lines changed: 33 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -41,77 +41,41 @@ The SDK offers APIs to abstract the Datastar protocol while allowing you to adap
4141
The following shows a simple implementation base of the Java `HttpServer`.
4242

4343
```kotlin
44-
45-
val server = HttpServer.create(
46-
InetSocketAddress(8080), // Port used
47-
0, // Backlog, 0 means default
48-
"/", // Path
49-
{ exchange -> // Exchange handler
50-
51-
// The `readSignals` method extracts the signals from the request.
52-
// If you use a web framework, you likely don't need this since the framework probably already handles this in its own way.
53-
// However, this method in the SDK allows you to provide your own unmarshalling strategy so you can adapt it to your preferred technology!
54-
val request: Request = adaptRequest(exchange)
55-
val signals = readSignals<EventsWrapper>(request, jsonUnmarshaller)
56-
57-
// Connect a Datastar SSE generator to the response.
58-
val response: Response = adaptResponse(exchange)
59-
val generator = ServerSentEventGenerator(response)
60-
61-
62-
// Below are some simple examples of how to use the generator.
63-
generator.patchElements(
64-
elements = "<div>Merge</div>",
65-
)
66-
67-
generator.patchSignals(
68-
signals =
69-
"""
70-
{
71-
"one":1,
72-
"two":2
73-
}
74-
""".trimIndent(),
75-
)
76-
77-
generator.executeScript(
78-
script = "alert('Hello World!')",
79-
)
80-
81-
exchange.close()
82-
}
44+
// Depending on your context, you'll need to adapt the `Request` and `Response` interfaces, as well as implementation of the `JsonUnmarshaller` type.
45+
val jsonUnmarshaller: JsonUnmarshaller<YourType> = ...
46+
val request: Request = ...
47+
val response: Response = ...
48+
49+
// The `readSignals` method extracts the signals from the request.
50+
// If you use a web framework, you likely don't need this since the framework probably already handles this in its own way.
51+
// However, this method in the SDK allows you to provide your own unmarshalling strategy so you can adapt it to your preferred technology!
52+
val request: Request = adaptRequest(exchange)
53+
val signals = readSignals<EventsWrapper>(request, jsonUnmarshaller)
54+
55+
// Connect a Datastar SSE generator to the response.
56+
val response: Response = adaptResponse(exchange)
57+
val generator = ServerSentEventGenerator(response)
58+
59+
// Below are some simple examples of how to use the generator.
60+
generator.patchElements(
61+
elements = "<div>Merge</div>",
8362
)
8463

85-
fun adaptRequest(exchange: HttpExchange): Request = object : Request {
86-
override fun bodyString() = exchange.requestBody.use { it.readAllBytes().decodeToString() }
87-
88-
override fun isGet() = exchange.requestMethod == "GET"
89-
90-
override fun readParam(string: String) =
91-
exchange.requestURI.query
92-
?.let { URLDecoder.decode(it, Charsets.UTF_8) }
93-
?.split("&")
94-
?.find { it.startsWith("$string=") }
95-
?.substringAfter("=")!!
96-
}
97-
98-
fun adaptResponse(exchange: HttpExchange): Response = object : Response {
99-
100-
override fun sendConnectionHeaders(
101-
status: Int,
102-
headers: Map<String, List<String>>,
103-
) {
104-
exchange.responseHeaders.putAll(headers)
105-
exchange.sendResponseHeaders(status, 0)
106-
}
64+
generator.patchSignals(
65+
signals =
66+
"""
67+
{
68+
"one":1,
69+
"two":2
70+
}
71+
""",
72+
)
10773

108-
override fun write(text: String) {
109-
exchange.responseBody.write(text.toByteArray())
110-
}
74+
generator.executeScript(
75+
script = "alert('Hello World!')",
76+
)
77+
```
11178

112-
override fun flush() {
113-
exchange.responseBody.flush()
114-
}
79+
### Examples
11580

116-
}
117-
```
81+
You can find runnable examples of how to use the SDK in multiple concrete web application frameworks and contexts in the [examples](examples/README.md) folder.

examples/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Datastar Kotlin Examples
2+
3+
* [About the examples](#about-the-examples)
4+
* [Running the examples](#running-the-examples)
5+
* [Prerequisites](#prerequisites)
6+
* [Java HTTP Server](#java-http-server)
7+
8+
## About the examples
9+
10+
This directory contains examples of using Datastar in Kotlin.
11+
All are a simple counter implemented using Datastar server-sent events.
12+
13+
- the front end consists in a single HTML page located in the `front` directory
14+
- each back implementation is in a separate directory, consisting in a single Kotlin file
15+
16+
## Running the examples
17+
18+
### Prerequisites
19+
20+
To make the examples work as simply as possible, each back implementation is a JBang script.
21+
22+
JBang is a tool that allows to run Kotlin scripts taking care of all the dependencies without the need to use more heavy weight tools like Maven or Gradle.
23+
You can find the installation instructions on the [official documentation](https://www.jbang.dev/documentation/jbang/latest/installation.html).
24+
25+
### Java HTTP Server
26+
27+
This example uses the plain Java `HttpServer` to serve the front end. ([code](java-httpserver/server.kt))
28+
29+
```shell
30+
cd ./java-httpserver ; jbang server.kt ; cd ..
31+
```

examples/java-httpserver/JavaHttpServer.kt renamed to examples/java-httpserver/server.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ import java.util.concurrent.Executors
1515
//DEPS org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2
1616

1717

18-
fun main() {
19-
val server = server()
20-
server.start()
21-
println("Server started on port ${server.address.port}")
18+
fun main(): Unit = server().run {
19+
start()
20+
println("Let's go counting star... http://localhost:${address.port}")
2221
}
2322

2423
fun server(

sdk/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ publishing {
9797
licenses {
9898
license {
9999
name = "MIT"
100-
url = "https://mit-license.org/"
100+
url = "https://github.com/starfederation/datastar/blob/develop/LICENSE.md"
101101
}
102102
}
103103
developers {

0 commit comments

Comments
 (0)