-
-
Notifications
You must be signed in to change notification settings - Fork 360
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Kotlin Ktor Hello world example (#3621)
Part of #3611 Working in the TodoMVC demo application. I'll raise a follow up PR for that.
- Loading branch information
1 parent
9ea6d67
commit e068f58
Showing
4 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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
20
example/kotlinlib/web/1-hello-ktor/src/com/example/HelloKtor.kt
This file contains 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
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>") | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
example/kotlinlib/web/1-hello-ktor/test/src/com/example/HelloKtor.kt
This file contains 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
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>" | ||
} | ||
} | ||
}) |
This file contains 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