Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion codeSnippets/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ module("snippets", "deployment-ktor-plugin")
module("snippets", "tutorial-website-static")
module("snippets", "server-websockets-sharedflow")
module("snippets", "tutorial-client-get-started")
module("snippets", "tutorial-server-get-started")
module("snippets", "tutorial-server-web-application")
module("snippets", "server-websockets-serialization")
module("snippets", "client-websockets-serialization")
Expand Down
15 changes: 10 additions & 5 deletions codeSnippets/snippets/tutorial-server-get-started-maven/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
<name>tutorial-server-get-started-maven</name>
<description>tutorial-server-get-started-maven</description>
<properties>
<ktor_version>3.3.1</ktor_version>
<kotlin.code.style>official</kotlin.code.style>
<kotlin_version>2.2.20</kotlin_version>
<logback_version>1.5.18</logback_version>
<slf4j_version>2.0.12</slf4j_version>
<ktor_version>3.3.1</ktor_version>
<logback_version>1.4.14</logback_version>
<slf4j_version>2.0.9</slf4j_version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
<main.class>com.example.ApplicationKt</main.class>
<main.class>io.ktor.server.netty.EngineMain</main.class>
</properties>
<repositories>
</repositories>
Expand All @@ -35,6 +35,11 @@
<artifactId>logback-classic</artifactId>
<version>${logback_version}</version>
</dependency>
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-server-status-pages-jvm</artifactId>
<version>${ktor_version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down Expand Up @@ -111,7 +116,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<version>3.7.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example

import io.ktor.server.application.*

fun main(args: Array<String>) {
io.ktor.server.netty.EngineMain.main(args)
}

fun Application.module() {
configureRouting()
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.example.plugins
package com.example

import io.ktor.http.*
import io.ktor.http.ContentType
import io.ktor.server.application.*
import io.ktor.server.http.content.*
import io.ktor.server.http.content.staticResources
import io.ktor.server.plugins.statuspages.StatusPages
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.routing.get
import io.ktor.server.plugins.statuspages.*

fun Application.configureRouting() {
install(StatusPages) {
Expand All @@ -21,11 +20,14 @@ fun Application.configureRouting() {
call.respondText("Hello World!")
}

// ...

get("/test1") {
val text = "<h1>Hello From Ktor</h1>"
val type = ContentType.parse("text/html")
call.respondText(text, type)
}

get("/error-test") {
throw IllegalStateException("Too Busy")
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="trace">
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
<logger name="org.eclipse.jetty" level="INFO"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.example

import io.ktor.client.request.get
import io.ktor.client.statement.bodyAsText
import io.ktor.http.HttpStatusCode
import io.ktor.http.contentType
import io.ktor.server.testing.testApplication
import kotlin.test.Test
import kotlin.test.assertContains
import kotlin.test.assertEquals

class ApplicationTest {

@Test
fun testRoot() = testApplication {
application {
module()
}
val response = client.get("/")

assertEquals(HttpStatusCode.OK, response.status)
assertEquals("Hello World!", response.bodyAsText())
}

@Test
fun testNewEndpoint() = testApplication {
application {
module()
}

val response = client.get("/test1")

assertEquals(HttpStatusCode.OK, response.status)
assertEquals("html", response.contentType()?.contentSubtype)
assertContains(response.bodyAsText(), "Hello From Ktor")
}
}

This file was deleted.

8 changes: 3 additions & 5 deletions codeSnippets/snippets/tutorial-server-get-started/README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
# Getting started with a Ktor Server

A sample project created in
A sample standalone project created in
the [Create, open and run a new Ktor project](https://ktor.io/docs/server-create-a-new-project.html)
tutorial.

> This sample is part of the [codeSnippets](../../README.md) Gradle project.

## Run

To run the sample, execute the following command in a repository's root directory:
To run the sample, execute the following command in the project's root directory:

```bash
./gradlew :tutorial-server-get-started:run
./gradlew run
```

Navigate to [http://0.0.0.0:8080/](http://0.0.0.0:8080/) to see the output.
37 changes: 16 additions & 21 deletions codeSnippets/snippets/tutorial-server-get-started/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
val ktor_version: String by project
val kotlin_version: String by project
val logback_version: String by project

plugins {
application
kotlin("jvm")
id("io.ktor.plugin") version "3.3.1"
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.ktor)
}

application {
mainClass.set("io.ktor.server.netty.EngineMain")
}
group = "com.example"
version = "0.0.1"

repositories {
mavenCentral()
maven { url = uri("https://maven.pkg.jetbrains.space/public/p/ktor/eap") }
application {
mainClass = "io.ktor.server.netty.EngineMain"
}

dependencies {
implementation("io.ktor:ktor-server-status-pages:$ktor_version")
implementation("io.ktor:ktor-server-core-jvm")
implementation("io.ktor:ktor-server-netty-jvm")
implementation("ch.qos.logback:logback-classic:$logback_version")
implementation("io.ktor:ktor-server-config-yaml:$ktor_version")
testImplementation("io.ktor:ktor-server-test-host-jvm:$ktor_version")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
}
// Added new dependency
implementation(libs.ktor.server.status.pages)
// Existing dependencies
implementation(libs.ktor.server.core)
implementation(libs.ktor.server.netty)
implementation(libs.logback.classic)
implementation(libs.ktor.server.config.yFaml)
testImplementation(libs.ktor.server.test.host)
testImplementation(libs.kotlin.test.junit)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kotlin.code.style=official
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[versions]
kotlin = "2.2.20"
ktor = "3.3.0"
logback = "1.4.14"

[libraries]
ktor-server-core = { module = "io.ktor:ktor-server-core-jvm", version.ref = "ktor" }
ktor-server-netty = { module = "io.ktor:ktor-server-netty", version.ref = "ktor" }
ktor-server-status-pages = { module = "io.ktor:ktor-server-status-pages", version.ref = "ktor" }
logback-classic = { module = "ch.qos.logback:logback-classic", version.ref = "logback" }
ktor-server-config-yaml = { module = "io.ktor:ktor-server-config-yaml", version.ref = "ktor" }
ktor-server-test-host = { module = "io.ktor:ktor-server-test-host", version.ref = "ktor" }
kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin" }

[plugins]
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
ktor = { id = "io.ktor.plugin", version.ref = "ktor" }
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.3-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading