Last active
November 4, 2025 19:47
-
-
Save sureshg/c4ca598b8c68afc79b6d54b7cbe3a42b to your computer and use it in GitHub Desktop.
Ktor App Kotlin Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env -S kotlin -Xplugin=/opt/homebrew/opt/kotlin/libexec/lib/kotlin-serialization-compiler-plugin.jar | |
| @file:DependsOn("io.ktor:ktor-server-core-jvm:3.3.1") | |
| @file:DependsOn("io.ktor:ktor-server-jetty-jakarta-jvm:3.3.1") | |
| @file:DependsOn("io.ktor:ktor-server-servlet-jakarta-jvm:3.3.1") | |
| @file:DependsOn("io.ktor:ktor-serialization-kotlinx-json-jvm:3.3.1") | |
| @file:DependsOn("org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.1") | |
| @file:DependsOn("ch.qos.logback:logback-classic:1.5.12") | |
| @file:CompilerOptions("-jvm-target", "21") | |
| import io.ktor.server.application.* | |
| import io.ktor.server.engine.* | |
| import io.ktor.server.jetty.jakarta.* | |
| import io.ktor.server.response.* | |
| import io.ktor.server.routing.* | |
| import io.ktor.server.servlet.jakarta.* | |
| import kotlinx.serialization.* | |
| import kotlinx.serialization.json.* | |
| @Serializable | |
| data class Info(val name: String, val version: String) | |
| val port = args.firstOrNull()?.toIntOrNull() ?: 9000 | |
| println("Starting Ktor 3.3.1 server with Kotlin ${KotlinVersion.CURRENT} on port $port...") | |
| embeddedServer(Jetty, port = port) { | |
| intercept(ApplicationCallPipeline.Call) { | |
| println("Attrs: ${call.request.servletRequestAttributes}") | |
| } | |
| routing { | |
| get("/info") { | |
| val json = Json.encodeToString(Info("Kotlin", KotlinVersion.CURRENT.toString())) | |
| call.respondText(json) | |
| } | |
| get("/") { | |
| call.respondText("OK") | |
| } | |
| } | |
| }.start(wait = true) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to Run
Install OpenJDK & Kotlin