https://kotlinlang.org logo
#http4k
Title
d

dave

09/14/2017, 10:27 AM
@pabl0rg we're looking at this now - I've spiked out a solution for this using Lenses. Here's the current version of how it might look:
Copy code
fun main(args: Array<String>) {

    val contexts = RequestContexts()

    val key = RequestContextKey.of<String>(contexts)
    val key2 = RequestContextKey.of<String>(contexts)

    fun AddStringTo(key: RequestContextLens<String>) = Filter { next ->
        {
            next(it.with(key of "hello", key2 of "bob"))
        }
    }

    val next: HttpHandler = { request ->
        Response(Status.OK).body(key.extract(request) + key2.extract(request))
    }

    val b = ServerFilters.InitialiseRequestContext(contexts)
        .then(AddStringTo(key))
        .then(next)

    val a = b(Request(Method.GET, "/hello"))
    println(a.bodyString())
}
. The
RequestContexts
is an object which is shared globally across the app. You also need to add the InitialiseRequestContext filter to the chain, which deals with cleaning up the RequestContext at the end.