Skip to content
Open
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
6 changes: 4 additions & 2 deletions src/main/kotlin/com/example/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package com.example

import com.example.config.configureDatabase
import com.example.config.configureDependencyInjection
import com.example.config.configureHttp
import com.example.config.configureLogging
import com.example.config.configureRouting
import com.example.config.configureSerialization
import com.example.domain.CafeMenuTable
import com.example.domain.repository.CafeMenuRepository
import io.ktor.server.application.*

fun main(args: Array<String>) {
Expand All @@ -15,6 +15,8 @@ fun main(args: Array<String>) {
fun Application.module() {
configureDatabase()
configureDependencyInjection()
configureHttp()
configureSerialization()
configureRouting()
configureLogging()
}
10 changes: 10 additions & 0 deletions src/main/kotlin/com/example/config/Http.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.config

import io.ktor.server.application.*
import io.ktor.server.plugins.doublereceive.*

fun Application.configureHttp() {
install(DoubleReceive)
install(ResponseDelayPlugin)
}

10 changes: 10 additions & 0 deletions src/main/kotlin/com/example/config/Logging.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.config


import com.example.config.plugin.MyCallLogging
import io.ktor.server.application.*

fun Application.configureLogging() {
// install(CallLogging)
install(MyCallLogging)
}
9 changes: 9 additions & 0 deletions src/main/kotlin/com/example/config/ResponseDelay.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.config

import io.ktor.server.application.*

val ResponseDelayPlugin = createApplicationPlugin(name = "ResponseDelayPlugin") {
onCall {
Thread.sleep(500)
}
}
45 changes: 45 additions & 0 deletions src/main/kotlin/com/example/config/plugin/MyCallLogging.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.example.config.plugin

import io.ktor.http.*
import io.ktor.http.content.*
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.util.logging.*
import org.slf4j.event.Level


internal val LOGGER = KtorSimpleLogger("[CallLogging]")
val MyCallLogging =
createRouteScopedPlugin("MyCallLogging") {
onCallRespond { call, body ->
if (!call.request.uri.startsWith("/api")) return@onCallRespond

LOGGER
.atLevel(getLevel(call))
.log(format(call, body))
}
}

fun getLevel(call: ApplicationCall): Level =
if (call.response.status()?.isSuccess() ?: true) Level.INFO else Level.ERROR

suspend fun format(call: ApplicationCall, body: Any): String {
val method = call.request.httpMethod.value
val uri = call.request.uri
val requestBody = call.receiveText()
.replace("\r\n", "")
.replace("\n", "")
.replace(" ", "")

val bodyStr = if (body is TextContent) {
"Response: ${body.text}"
} else {
""
}

val request = "Request: $method $uri, $requestBody"
val status = "${call.response.status()?.value ?: ""}"
val log = listOf(request, status, bodyStr).filter { it.isNotBlank() }.joinToString("\n")

return "\n" + log
}
10 changes: 10 additions & 0 deletions src/main/kotlin/com/example/config/plugin/ResponseDelay.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.config.plugin

import io.ktor.server.application.*


val ResponseDelayPlugin = createApplicationPlugin(name = "ResponseDelayPlugin") {
onCall {
Thread.sleep(500)
}
}