Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scaffold server with ktor as per issue #23 #30

Merged
merged 2 commits into from
Oct 4, 2023
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
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ subprojects {
plugin("org.jetbrains.dokka")
}



tasks.withType<Detekt>().configureEach {
jvmTarget = "1.8"
}
Expand Down
9 changes: 9 additions & 0 deletions http-server/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
plugins {
id("org.jetbrains.kotlin.jvm")
id("java-library")
id("idea")
}

version = "1.0"
var ktorVersion = "2.3.4"

repositories {
mavenCentral()
Expand All @@ -14,8 +16,15 @@ repositories {

dependencies {
testImplementation(kotlin("test"))
testImplementation("io.ktor:ktor-server-test-host:$ktorVersion")
implementation("io.ktor:ktor-server-core:$ktorVersion")
implementation("io.ktor:ktor-server-netty:$ktorVersion")
implementation("io.ktor:ktor-serialization:$ktorVersion")


}


tasks.test {
useJUnitPlatform()
}
Expand Down
Empty file.
43 changes: 43 additions & 0 deletions http-server/src/main/kotlin/Server.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package tbdex.server

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

/**
* Main entrypoint of the executable that starts a Netty webserver at port 8080
* and registers the [module].
*
*/

fun main() {
embeddedServer(Netty, port = 8080) { module() }.start(wait = true)
}

fun Application.module() {
routing {
get("/") {
call.respondText { "hello world" }
}
post("/manual") {
// AF4GH is a sample code for demo purposes
call.response.header("Location", "/manual/AF4GH")
call.response.status(HttpStatusCode.Created)
call.respondText("Manually setting the location header")
}
post("/extension") {
// AF4GH is a sample code for demo purposes
call.response.created("AF4GH")
call.respondText("Extension setting the location header")
}
}
}

private fun ApplicationResponse.created(id: String) {
call.response.status(HttpStatusCode.Created)
call.response.header("Location", "${call.request.uri}/$id")
}
Empty file.
31 changes: 31 additions & 0 deletions http-server/src/test/kotlin/ServerTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import io.ktor.server.application.*
import io.ktor.http.*
import io.ktor.server.testing.*
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
import tbdex.server.*

class ServerTest {

@Test
fun `test manual route`() {
withTestApplication(Application::module) {
handleRequest(HttpMethod.Post, "/manual").apply {
assertEquals(HttpStatusCode.Created, response.status())
assertEquals("/manual/AF4GH", response.headers["Location"])
assertEquals("Manually setting the location header", response.content)
}
}
}

@Test
fun `test extension route`() {
withTestApplication(Application::module) {
handleRequest(HttpMethod.Post, "/extension").apply {
assertEquals(HttpStatusCode.Created, response.status())
assertEquals("/extension/AF4GH", response.headers["Location"])
assertEquals("Extension setting the location header", response.content)
}
}
}
}
Loading