When trying to serve static resources via Ktor, it...
# ktor
m
When trying to serve static resources via Ktor, it seems like it's not possible to specify the classloader responsible for these resources? I can see that the
resolveResource
function underlying the
staticResources
function ultimately has the option to pass in the class loader, but
staticResources
itself doesn't have it. This is a modular application which loads functionality from plugins, so the Ktor classloader doesn't have access to the resources of the plugins. Any tips?
a
Unfortunately, I don't see easy ways to do that other than copy-pasting the code or setting another class loader in the application environment.
m
explicitly setting the Classloader in the
Application
might be an option... 🤔 It's not optimal as using the same class loader for all resources would result in issues when multiple plugins use the same name for some resources, though. My current plan was to have individual
staticResources
blocks, one for each plugin - with different paths, of course - and using the appropriate class loader. But out of curiosity: where would I have to set it? My
main
function currently looks only like
Copy code
fun main(args: Array<String>) {
    io.ktor.server.netty.EngineMain.main(args)
}
and this loads the configuration YAML that provides the module name and then ends up executing
Application.myModule()
Inside that module is the initialization code that also builds the classloader which wraps the classloaders of all the plugins and their resources.
a
I think you need to use the
embeddedServer
to specify the class loader.
👍 1