Is there a preferred way to handle a mutable colle...
# http4k
e
Is there a preferred way to handle a mutable collection of routes (in this case a map)? This is my initial solution but I feel like there may be a cleaner solution that I've overlooked.
j
Are you sure that you want a mutable set of routes? Maybe the thing that you're trying to do can be achieved a different way?
e
I think so. I'm working with plugins that can be loaded and unloaded at runtime, not just enabled/disabled, with each plugin containing their own routes. So unfortunately I don't know about all potential plugins/routes at application/server start.
j
So maybe the thing you want is a fixed "plugin" route handler? If you just go with a map, I think you might get stuck into classloader issues quickly... the plugin route handler would then delegate to the routes exposed by the plugin.. but the top level route itself doesn't change.
e
I've got the classloader stuff already sorted, this was just a simplified sample. In practice my route handler looks more like below, where the map comes from a computed property at request time. But yes, I was also thinking of delegating. I guess a better question to ask would've been 'What is the preferred way to delegate a request from one route to another?' Cause the way I'm doing it currently I need to wrap the route I'm delegating to with the full path inside the handler since the plugin route doesn't know about any higher parts of the path. Like in the sample I need to wrap the route I'm delegating to with
routes("/myroutes" bind it)
before delegating to it
Copy code
fun <T: Plugin> pluginHandler(manager: PluginManager<T>): HttpHandler {
    val plugin = Path.of("plugin")

    return handler@{ req: Request ->
        val pluginRouteMap = manager.loadedPluginRouteMap
        val plugin = plugin(req)
        val route = pluginRouteMap[plugin]
        if (route == null) {
            return@handler Response(NOT_ACCEPTABLE)
        }

        routes("/plugins" bind route)(req)
    }
}