/kotlin-worker-hello-world

Cloudflare worker with KotlinJS 1.9.0

Primary LanguageKotlinApache License 2.0Apache-2.0

Kotlin hello world for Cloudflare Workers

Your Kotlin code in main.kt, running on Cloudflare Workers

In addition to 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.

Wrangler 3.9.0

To generate using wrangler

Note: This still uses Service Workers API instead of the newer module syntax

Kotlin JS doesn't support module syntax with default export yet, so we need to use the Service Workers API for now.

// new module syntax with default not supported yet in Kotlin JS 
export default {
   async fetch(request, env, ctx) {
       return new Response('Hello World!');
   },
};
wrangler generate projectname https://github.com/griffio/kotlin-worker-hello-world

You need to register a workers.dev subdomain before running the dev command in remote mode

Further documentation for Wrangler can be found here.

Gradle

After setting up Kotlin per the linked instructions above,

cd projectname
./gradlew build

That will compile your code and package it into build/distributions/kotlin-worker-hello-world.js, after which you can run wrangler deploy build/distributions/kotlin-worker-hello-world.js to push it to Cloudflare.