https://kotlinlang.org logo
Title
h

Henry

05/07/2020, 8:01 AM
basic one but what's the best pattern for accessing config in top level functions (or is this just not the way to do things)? e.g.
fun fetchInternalClientInstance() { ...I need access to application.config in here...}
on a wider point could anyone link me to some resources discussing higher level application architecture with ktor as I'm starting to get beyond what is covered by the Youkube example?
👍 2
Similarly it'd also be useful to get access to the application coroutine context which is also accessible through the application instance
d

David Eriksson

05/07/2020, 12:17 PM
I suggest you try to follow the setup pattern for ktor itself
👍 1
h

Henry

05/07/2020, 12:19 PM
makes sense, it's just a big shift coming from a service-oriented design!
d

David Eriksson

05/07/2020, 12:20 PM
I'm trying to find a suitable example...
Maybe
Depends a bit what you need the config for 🙂
Maybe it's not a Feature (similar to a middleware in ExpressJs or .NET MVC) you want the config for
But by writing extension functions to
Application
you have all its context
m

mp

05/07/2020, 2:05 PM
personally I don't like the way the ktor project guides you towards doing config. I suggest just using
embeddedServer
in a main method, and then you can do config any way you please. Load from env vars, or property files, or whatever, at startup and distribute the resulting config object(s) as you see fit
I think it's a mistake to bolt that sort of lifecycle stuff (reading config out of files) into an HTTP server lib.
h

Henry

05/07/2020, 2:06 PM
thanks for responses
I think it's a mistake to bolt that sort of lifecycle stuff (reading config out of files) into an HTTP server lib.
this is possibly true but I'm seeing what I can do with a pure ktor implementation for now
(there are benefits to a tightly integrated config system as well imo)
d

David Eriksson

05/07/2020, 2:07 PM
Framework vs library @mp? 🙂
💯 1
h

Henry

05/07/2020, 2:07 PM
@David Eriksson I suppose making all my top level functions extensions on application is an option. They then remain callable from routes etc.
d

David Eriksson

05/07/2020, 2:08 PM
I think that's the ktor way
h

Henry

05/07/2020, 2:08 PM
makes sense
I'll give it a go, thanks for the tip!
d

David Eriksson

05/07/2020, 2:10 PM
It feels pretty strange compared to the "class-based" like Spring Boot or DropWizard :)
h

Henry

05/07/2020, 2:11 PM
yeah it's an interesting shift in mindset
m

mp

05/07/2020, 2:12 PM
The extension function way breaks down as the app grows because you'd need all the various support objects (Db connections, etc) in scope.
h

Henry

05/07/2020, 2:12 PM
I can well imagine that happening
m

mp

05/07/2020, 2:12 PM
I maintain a demo project showing patterns I've used in other (production) ktor services: https://bitbucket.org/marshallpierce/ktor-demo/src/master/
🤩 1
👍 1
h

Henry

05/07/2020, 2:12 PM
I'm already wondering about the best way to use kodein with this pattern as it's straightforward with service constructor injection
m

mp

05/07/2020, 2:13 PM
key principle: every component is replacable, as much as possible. Obviously ripping out ktor is going to be more of a pain since all the url routing would change, but you could do it. Same for everything else.
Don't like the way that project does config? Or json mapping? or whatever? select the corresponding code, press delete, do what you like.
that said, the components I've chosen all play nicely together, and if you don't want to worry about every detail, you won't hate it if you just go with what I've picked.
That's using Guice but the same idea would work for anything
h

Henry

05/07/2020, 2:18 PM
oh so you're using DI to inject the application object
that did seem like a viable alternative
removes the need to make all your service functions extension functions as well
well thanks again everyone
will go off and build some things
and see what happens
m

mp

05/07/2020, 2:20 PM
best way to learn!
h

Henry

05/07/2020, 2:20 PM
quite!
d

David Eriksson

05/07/2020, 2:24 PM
Your demo is very interesting @mp, thanks for sharing... there is certainly more than one way to do things 🙂
👍 1