basic one but what's the best pattern for accessin...
# ktor
h
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.
Copy code
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
I suggest you try to follow the setup pattern for ktor itself
๐Ÿ‘ 1
h
makes sense, it's just a big shift coming from a service-oriented design!
d
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
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
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
Framework vs library @mp? ๐Ÿ™‚
๐Ÿ’ฏ 1
h
@David Eriksson I suppose making all my top level functions extensions on application is an option. They then remain callable from routes etc.
d
I think that's the ktor way
h
makes sense
I'll give it a go, thanks for the tip!
d
It feels pretty strange compared to the "class-based" like Spring Boot or DropWizard :)
h
yeah it's an interesting shift in mindset
m
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
I can well imagine that happening
m
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
I'm already wondering about the best way to use kodein with this pattern as it's straightforward with service constructor injection
m
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
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
best way to learn!
h
quite!
d
Your demo is very interesting @mp, thanks for sharing... there is certainly more than one way to do things ๐Ÿ™‚
๐Ÿ‘ 1