hey :wave: - maybe silly question but how do I com...
# http4k
a
hey 👋 - maybe silly question but how do I combine two HttpHandlers, I can do it myself but wondering if theres a http4k way? e.g.
Copy code
val handler1 = routes("foo" bind GET to { Response(OK) })
val handler2 = routes("bar" bind GET to { Response(OK) })
val combined = handler1 combinedWith handler2
Copy code
class Testing {
    @Test
    fun `works`() {
        val handler1 = routes("foo" bind GET to { Response(OK) })
        val handler2 = routes("bar" bind GET to { Response(OK) })

        val combined = handler1 combinedWith handler2

        assertThat(combined(Request(GET, "/foo")).status, equalTo(OK))
        assertThat(combined(Request(GET, "/bar")).status, equalTo(OK))
    }
}

infix fun HttpHandler.combinedWith(other: HttpHandler): HttpHandler = { request ->
    val response = this(request)
    if (response.status == Status.NOT_FOUND) {
        other(request)
    } else {
        response
    }
}
d
you can compose multiple routes inside a single routes block
a
aha
Copy code
val combined = routes(handler1, handler2)
did it - that's what I was looking for.. thanks
👍 1