Skip to content

Commit ed53558

Browse files
committed
Fix improper handling of text/plain responses
Add the ability to enable request/response debugging
1 parent fdf5cae commit ed53558

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

src/main/kotlin/com/batchofcode/runtimelocal/config/EnvConfig.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ package com.batchofcode.runtimelocal.config
22

33
object EnvConfig {
44
val port = System.getProperty("runtime.port")?.toInt() ?: System.getenv("RUNTIME_PORT")?.toInt() ?: 9000
5+
val debug = System.getProperty("runtime.debug")?.toBoolean() ?: System.getenv("RUNTIME_DEBUG")?.toBoolean() ?: false
56
}

src/main/kotlin/com/batchofcode/runtimelocal/handler/InvocationHandler.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import org.http4k.core.Status.Companion.BAD_REQUEST
99
import org.http4k.core.Status.Companion.OK
1010
import org.http4k.format.Jackson.json
1111
import org.http4k.lens.Header
12+
import org.http4k.lens.LensFailure
1213
import org.http4k.lens.string
1314
import org.http4k.routing.RoutingHttpHandler
1415
import org.http4k.routing.bind
@@ -44,7 +45,12 @@ object InvocationHandler {
4445
}
4546

4647
fun response(): HttpHandler = handler@{
47-
val requestBody = Body.json().toLens()(it)
48+
val requestBody = try {
49+
Body.json().toLens()(it).toString()
50+
}
51+
catch (ex: LensFailure) {
52+
Body.string(ContentType.TEXT_PLAIN).toLens()(it)
53+
}
4854
val traceIdHeader = Header.optional("_X_AMZN_TRACE_ID")(it)
4955
val requestId = it.path("requestId") ?: return@handler Response(BAD_REQUEST).with(
5056
Body.string(ContentType.TEXT_PLAIN).toLens() of "Missing Request ID"

src/main/kotlin/com/batchofcode/runtimelocal/handler/RootHandler.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@ package com.batchofcode.runtimelocal.handler
22

33
import com.batchofcode.runtimelocal.config.EnvConfig
44
import org.http4k.core.then
5+
import org.http4k.filter.DebuggingFilters
56
import org.http4k.filter.ServerFilters
67
import org.http4k.server.ApacheServer
78
import org.http4k.server.asServer
89

910
fun main() {
1011
val port = EnvConfig.port
1112

12-
ServerFilters.CatchAll()
13+
var filterChain = ServerFilters.CatchAll()
14+
if (EnvConfig.debug) {
15+
println("REQUEST/RESPONSE DEBUG MODE ENABLED")
16+
filterChain = filterChain.then(DebuggingFilters.PrintRequestAndResponse())
17+
}
18+
19+
filterChain
1320
.then(Routes())
1421
.asServer(ApacheServer(port)).start()
1522

0 commit comments

Comments
 (0)