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

Nezteb

04/22/2019, 11:01 PM
Are there any examples of using http4k with an
AutoCloseable
thing like a database client that needs to be opened upon request and closed once the response is sent? Or using
try-with-resources
?
✔️ 1
s

s4nchez

04/22/2019, 11:09 PM
Not that I'm aware of. The normal approach I see is managing the db resource in the boundary of the service/repository, rather than at the handler level.
The pattern I recommend is using handlers primarily to extract/inject data from/into Request/Response, but rely on other domain abstractions to deal with other concerns.
The "http as boundary of everything" is a bit of an anti-pattern in my view.
n

Nezteb

04/22/2019, 11:14 PM
True; I’m going off of this. https://github.com/http4k/http4k-by-example/blob/master/src/test/kotlin/env/FakeUserDirectory.kt#L32 Right now I’m porting an app that was already built over to http4k to prove it out for some architects.
And it unfortunately is very coupled to the DB implementation
s

s4nchez

04/22/2019, 11:18 PM
You can replace
private val users = mutableMapOf<Id, User>()
with a class that deals with set/get/delete of users with the db resource management encapsulated in it.
You can even make it "look" like a map by using operator functions (i.e.
users[newUser.id] = newUser
happens as a transaction)
n

Nezteb

04/22/2019, 11:22 PM
I’m looking at that now. Thank you for the suggestions! 🙂
s

s4nchez

04/22/2019, 11:23 PM
[...] to prove it out for some architects.
Feel free to point them to this channel if they have any particular concerns they want to address as well 😉
n

Nezteb

04/22/2019, 11:31 PM
I’ll be giving a talk at work about it as soon as I’m done with this service. 😄
s

s4nchez

04/22/2019, 11:58 PM
Cool! Hope it goes well 🙂
d

dave

04/23/2019, 4:23 AM
@Nezteb not sure if you've seen it, but check out the KotlinConf talk on http4k. It's got a bunch of stuff that might be useful as ideas for your talk - the second half especially concerns more systems level stuff like testing improvements: https://bit.ly/serverasafunction
3 Views