org.http4k.contract.Module.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of http4k-contract Show documentation
Show all versions of http4k-contract Show documentation
http4k typesafe HTTP contracts and OpenApi support
package org.http4k.contract
import org.http4k.core.HttpHandler
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status.Companion.BAD_REQUEST
import org.http4k.core.Status.Companion.NOT_FOUND
import org.http4k.lens.LensFailure
typealias Router = (Request) -> HttpHandler?
interface Module {
infix fun then(that: Module): Module {
val thisBinding = toRouter()
val thatBinding = that.toRouter()
return object : Module {
override fun toRouter(): Router = { req -> thisBinding(req) ?: thatBinding(req) }
}
}
fun toHttpHandler(): HttpHandler {
val handlerMatcher = toRouter()
return { req ->
handlerMatcher(req)?.let {
try {
it(req)
} catch (e: LensFailure) {
Response(BAD_REQUEST)
}
} ?: Response(NOT_FOUND)
}
}
fun toRouter(): Router
}