Skip to content

Commit

Permalink
Update for Wrangler 2 and Kotlin 1.7.10
Browse files Browse the repository at this point in the history
This uses the newer Worker syntax and removes some of the bespoke
browser work now that Kotlin better supports JavaScript.
  • Loading branch information
SalvatoreT committed Sep 14, 2022
1 parent 195e97c commit 8c574d8
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 55 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ node_modules/
.cargo-ok
/index.js
/build/
.idea
.gradle
.DS_Store
kotlin-js-store
21 changes: 10 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@

Your Kotlin code in [main.kt](https://github.com/cloudflare/kotlin-worker-hello-world/blob/master/src/main/kotlin/main.kt), running on Cloudflare Workers

In addition to [Wrangler](https://github.com/cloudflare/wrangler) you will need to install Kotlin, including a JDK and support for Gradle projects. The easiest way to do this is using the free Community Edition of [IntelliJ IDEA](https://kotlinlang.org/docs/tutorials/jvm-get-started.html).
In addition to [Wrangler v2.x](https://github.com/cloudflare/wrangler2) you will need to install Kotlin, including a JDK and support for Gradle projects. The easiest way to do this is using the free Community Edition of [IntelliJ IDEA](https://kotlinlang.org/docs/tutorials/jvm-get-started.html).

#### Wrangler
## Wrangler

To generate using [wrangler](https://github.com/cloudflare/wrangler)

```
wrangler generate projectname https://github.com/cloudflare/kotlin-worker-hello-world
```
Configure the [wrangler.toml](wrangler.toml) by filling in the `account_id` from the Workers pages of your Cloudflare Dashboard.

Further documentation for Wrangler can be found [here](https://developers.cloudflare.com/workers/tooling/wrangler).

#### Gradle
## Gradle

After setting up Kotlin per the linked instructions above,

```
cd projectname
./gradlew buildWorker
./gradlew :compileProductionExecutableKotlinJs
```

That will compile your code and package it into index.js, after which you can run `wrangler publish` to push it to Cloudflare.
That will compile your code and package it into a JavaScript executable, after which you can run `wrangler publish` to push it to Cloudflare.

```
wrangler publish build/js/packages/kotlin-worker-hello-world/kotlin/kotlin-worker-hello-world.js
```

For more information on interop between Kotlin and Javascript, see the [Kotlin docs](https://kotlinlang.org/docs/reference/js-interop.html). Regarding coroutines, see [this issue and workaround](https://github.com/cloudflare/kotlin-worker-hello-world/issues/2)
21 changes: 6 additions & 15 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id("org.jetbrains.kotlin.js") version "1.3.72"
kotlin("js") version "1.7.10"
}

group = "org.example"
Expand All @@ -9,19 +9,10 @@ repositories {
mavenCentral()
}

dependencies {
implementation(kotlin("stdlib-js"))
testImplementation(kotlin("test-js"))
}

kotlin.target.browser {
}


tasks.register("buildWorker") {
dependsOn("browserProductionWebpack")
doLast {
/* Kotlin js output assumes window exists, which it won't on Workers. Hack around it */
file("$projectDir/index.js").writeText("const window=this; " + file("$projectDir/build/distributions/${rootProject.name}.js").readText())
kotlin {
js(IR) {
nodejs {
}
binaries.executable()
}
}
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
kotlin.code.style=official
kotlin.code.style=official
kotlin.js.ir.output.granularity=whole-program
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
{
"name": "{{ project-name }}",
"name": "kotlin-worker-hello-world",
"version": "1.0.0",
"description": "Kotlin hello world for Cloudflare Workers",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "{{ authors }}",
"license": "MIT",
"devDependencies": {
}
"license": "MIT"
}
2 changes: 0 additions & 2 deletions settings.gradle

This file was deleted.

27 changes: 10 additions & 17 deletions src/main/kotlin/main.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import org.w3c.fetch.Request
import org.w3c.fetch.Response
import org.w3c.fetch.ResponseInit
import org.w3c.workers.FetchEvent
import kotlin.js.Promise

external fun addEventListener(type: String, f: (FetchEvent) -> Unit)

fun main() {
addEventListener("fetch") { event: FetchEvent ->
val headers: dynamic = object {}
headers["content-type"] = "text/plain"
event.respondWith(
Promise.resolve(
Response(
"Kotlin Worker hello world",
ResponseInit(headers = headers)
)
)
)
}
@OptIn(ExperimentalJsExport::class)
@JsExport
fun fetch(request: Request) : Response {
val headers: dynamic = object {}
headers["content-type"] = "text/plain"
return Response(
"Kotlin Worker hello world",
ResponseInit(headers = headers)
)
}
2 changes: 1 addition & 1 deletion src/main/resources/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ project-name }}</title>
<title>kotlin-worker-hello-world</title>
<script src="{{ project-name}}.js"></script>
</head>
<body>
Expand Down
4 changes: 1 addition & 3 deletions wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
name = ""
type = "javascript"
account_id = ""
workers_dev = true
route = ""
zone_id = ""
compatibility_date = "2022-08-11"

0 comments on commit 8c574d8

Please sign in to comment.