Skip to content

Commit

Permalink
Add Kotlin Ktor Hello world example (#3621)
Browse files Browse the repository at this point in the history
Part of #3611

Working in the TodoMVC demo application. I'll raise a follow up PR for
that.
  • Loading branch information
javimartinez authored Sep 28, 2024
1 parent 9ea6d67 commit e068f58
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
38 changes: 38 additions & 0 deletions example/kotlinlib/web/1-hello-ktor/build.mill
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//// SNIPPET:BUILD
package build
import mill._, kotlinlib._

object `package` extends RootModule with KotlinModule {

def kotlinVersion = "1.9.24"

def mainClass = Some("com.example.HelloKtorKt")

def ivyDeps = Agg(
ivy"io.ktor:ktor-server-core-jvm:2.3.12",
ivy"io.ktor:ktor-server-netty-jvm:2.3.12"
)

object test extends KotlinModuleTests with TestModule.Junit5 {
def ivyDeps = super.ivyDeps() ++ Agg(
ivy"io.kotest:kotest-runner-junit5-jvm:5.9.1",
ivy"io.ktor:ktor-server-test-host-jvm:2.3.12"
)
}
}

// This example demonstrates how to set up a simple webserver
// serving a single "<h1>Hello, World!</h1>" web page using Kotlin and Ktor

/** Usage

> mill test

> mill runBackground

> curl http://localhost:8080
...<h1>Hello, World!</h1>...

> mill clean runBackground

*/
20 changes: 20 additions & 0 deletions example/kotlinlib/web/1-hello-ktor/src/com/example/HelloKtor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example

import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.response.*
import io.ktor.server.routing.*

fun main() {
embeddedServer(Netty, port = 8080, host = "0.0.0.0", module = Application::module)
.start(wait = true)
}

fun Application.module() {
routing {
get("/") {
call.respondText("<h1>Hello, World!</h1>")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example

import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.server.testing.*

import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe

class HelloKtorTest: FunSpec({
test("HelloKtorTest") {
testApplication {
application {
module()
}
val response = client.get("/")
response.status shouldBe HttpStatusCode.OK
response.bodyAsText() shouldBe "<h1>Hello, World!</h1>"
}
}
})
1 change: 1 addition & 0 deletions example/package.mill
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ object `package` extends RootModule with Module {
object module extends Cross[ExampleCrossModuleKotlin](build.listIn(millSourcePath / "module"))
object publishing extends Cross[ExampleCrossModuleKotlin](build.listIn(millSourcePath / "publishing"))
object testing extends Cross[ExampleCrossModuleKotlin](build.listIn(millSourcePath / "testing"))
object web extends Cross[ExampleCrossModuleKotlin](build.listIn(millSourcePath / "web"))
}
object scalalib extends Module {
object basic extends Cross[ExampleCrossModule](build.listIn(millSourcePath / "basic"))
Expand Down

0 comments on commit e068f58

Please sign in to comment.