dave
02/18/2018, 9:57 PMimport org.http4k.core.HttpTransaction
import org.http4k.core.Method.DELETE
import org.http4k.core.Method.GET
import <http://org.http4k.core.Method.POST|org.http4k.core.Method.POST>
import org.http4k.core.Method.PUT
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status.Companion.OK
import org.http4k.core.then
import org.http4k.filter.ResponseFilters
import org.http4k.routing.bind
import org.http4k.routing.routes
data class Dimension(val a: String, val v: String)
fun emitMetric(listOf: List<Dimension>, s: String, toMillis: Long) {
println(listOf + s + toMillis)
}
val emit = { tx: HttpTransaction ->
emitMetric(listOf(
Dimension("URI", tx.routingGroup),
Dimension("Method", tx.label("Name")!!)
), "Latency", tx.duration.toMillis()
)
}
val mapsRoute = routes(
"/{id}" bind PUT to name("UpdateMap").then { _: Request -> Response(OK) },
"/" bind POST to name("CreateMap").then { _: Request -> Response(OK) },
"/{id}" bind DELETE to name("DeleteMap").then { _: Request -> Response(OK) },
"/{id}" bind GET to name("ReadMap").then { _: Request -> Response(OK) }
)
val app = routes(
"/maps" bind mapsRoute
// More routes here
)
fun name(name: String) = ResponseFilters.ReportHttpTransaction(
transactionLabeller = { tx: HttpTransaction ->
tx.label("Name", name)
}, recordFn = emit
)
fun main(args: Array<String>) {
app(Request(GET, "/maps/123"))
app(Request(POST, "/maps/"))
}