diff --git a/src/main/kotlin/com/example/Application.kt b/src/main/kotlin/com/example/Application.kt index 5d10ce2..f5cd917 100644 --- a/src/main/kotlin/com/example/Application.kt +++ b/src/main/kotlin/com/example/Application.kt @@ -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) { @@ -15,6 +15,8 @@ fun main(args: Array) { fun Application.module() { configureDatabase() configureDependencyInjection() + configureHttp() configureSerialization() configureRouting() + configureLogging() } diff --git a/src/main/kotlin/com/example/config/Http.kt b/src/main/kotlin/com/example/config/Http.kt new file mode 100644 index 0000000..1c24e3c --- /dev/null +++ b/src/main/kotlin/com/example/config/Http.kt @@ -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) +} + diff --git a/src/main/kotlin/com/example/config/Logging.kt b/src/main/kotlin/com/example/config/Logging.kt new file mode 100644 index 0000000..310ef01 --- /dev/null +++ b/src/main/kotlin/com/example/config/Logging.kt @@ -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) +} diff --git a/src/main/kotlin/com/example/config/ResponseDelay.kt b/src/main/kotlin/com/example/config/ResponseDelay.kt new file mode 100644 index 0000000..a283473 --- /dev/null +++ b/src/main/kotlin/com/example/config/ResponseDelay.kt @@ -0,0 +1,9 @@ +package com.example.config + +import io.ktor.server.application.* + +val ResponseDelayPlugin = createApplicationPlugin(name = "ResponseDelayPlugin") { + onCall { + Thread.sleep(500) + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/config/plugin/MyCallLogging.kt b/src/main/kotlin/com/example/config/plugin/MyCallLogging.kt new file mode 100644 index 0000000..6e11862 --- /dev/null +++ b/src/main/kotlin/com/example/config/plugin/MyCallLogging.kt @@ -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 +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/config/plugin/ResponseDelay.kt b/src/main/kotlin/com/example/config/plugin/ResponseDelay.kt new file mode 100644 index 0000000..f5a5ed7 --- /dev/null +++ b/src/main/kotlin/com/example/config/plugin/ResponseDelay.kt @@ -0,0 +1,10 @@ +package com.example.config.plugin + +import io.ktor.server.application.* + + +val ResponseDelayPlugin = createApplicationPlugin(name = "ResponseDelayPlugin") { + onCall { + Thread.sleep(500) + } +}