Are there any examples of using http4k with an `Au...
# http4k
n
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
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
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
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
I’m looking at that now. Thank you for the suggestions! 🙂
s
[...] 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
I’ll be giving a talk at work about it as soon as I’m done with this service. 😄
s
Cool! Hope it goes well 🙂
d
@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