Skip to content

Commit

Permalink
ktor things
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssaruth committed Sep 4, 2024
1 parent 03bfde9 commit 6712e84
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 13 deletions.
14 changes: 13 additions & 1 deletion server/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
plugins {
id("Entropy.kotlin-common-conventions")
id("com.ncorti.ktfmt.gradle") version "0.15.1"
id("io.ktor.plugin") version "2.3.12"
application
}

ktfmt { kotlinLangStyle() }

dependencies {
implementation(project(":core"))
implementation("io.ktor:ktor-server-core-jvm")
implementation("io.ktor:ktor-server-content-negotiation-jvm")
implementation("io.ktor:ktor-serialization-jackson-jvm")
implementation("io.ktor:ktor-server-call-logging-jvm")
implementation("io.ktor:ktor-server-call-id-jvm")
implementation("io.ktor:ktor-server-host-common-jvm")
implementation("io.ktor:ktor-server-status-pages-jvm")
implementation("io.ktor:ktor-server-sessions-jvm")
implementation("io.ktor:ktor-server-netty-jvm")
implementation("io.ktor:ktor-server-config-yaml")
testImplementation("io.ktor:ktor-server-test-host-jvm")
testImplementation(project(":test-core"))
}

application {
// Define the main class for the application.
mainClass.set("server.EntropyServer")
mainClass.set("io.ktor.server.netty.EngineMain")
}
4 changes: 4 additions & 0 deletions server/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
kotlin.code.style=official
ktor_version=2.3.12
kotlin_version=2.0.20
logback_version=1.4.14
12 changes: 0 additions & 12 deletions server/src/main/java/util/MainUtil.java

This file was deleted.

17 changes: 17 additions & 0 deletions server/src/main/kotlin/Application.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import plugins.*
import io.ktor.server.application.*
import server.EntropyServer

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

fun Application.module() {
configureSerialization()
configureMonitoring()
configureSecurity()
configureRouting()

// Boot old stuff
EntropyServer.main(emptyArray())
}
22 changes: 22 additions & 0 deletions server/src/main/kotlin/plugins/Monitoring.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package plugins

import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.plugins.callid.*
import io.ktor.server.plugins.callloging.*
import io.ktor.server.request.*
import org.slf4j.event.*

fun Application.configureMonitoring() {
install(CallLogging) {
level = Level.INFO
filter { call -> call.request.path().startsWith("/") }
callIdMdc("call-id")
}
install(CallId) {
header(HttpHeaders.XRequestId)
verify { callId: String ->
callId.isNotEmpty()
}
}
}
20 changes: 20 additions & 0 deletions server/src/main/kotlin/plugins/Routing.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package plugins

import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.plugins.statuspages.*
import io.ktor.server.response.*
import io.ktor.server.routing.*

fun Application.configureRouting() {
install(StatusPages) {
exception<Throwable> { call, cause ->
call.respondText(text = "500: $cause" , status = HttpStatusCode.InternalServerError)
}
}
routing {
get("/") {
call.respondText("Hello World!")
}
}
}
22 changes: 22 additions & 0 deletions server/src/main/kotlin/plugins/Security.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package plugins

import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.sessions.*

fun Application.configureSecurity() {
data class MySession(val count: Int = 0)
install(Sessions) {
cookie<MySession>("MY_SESSION") {
cookie.extensions["SameSite"] = "lax"
}
}
routing {
get("/session/increment") {
val session = call.sessions.get<MySession>() ?: MySession()
call.sessions.set(session.copy(count = session.count + 1))
call.respondText("Counter is ${session.count}. Refresh to increment.")
}
}
}
21 changes: 21 additions & 0 deletions server/src/main/kotlin/plugins/Serialization.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package plugins

import com.fasterxml.jackson.databind.*
import io.ktor.serialization.jackson.*
import io.ktor.server.application.*
import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.server.response.*
import io.ktor.server.routing.*

fun Application.configureSerialization() {
install(ContentNegotiation) {
jackson {
enable(SerializationFeature.INDENT_OUTPUT)
}
}
routing {
get("/json/jackson") {
call.respond(mapOf("hello" to "world"))
}
}
}
6 changes: 6 additions & 0 deletions server/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ktor:
application:
modules:
- ApplicationKt.module
deployment:
port: 8080

0 comments on commit 6712e84

Please sign in to comment.