import io.ktor.server.engine.* import io.ktor.server.netty.* import io.ktor.server.response.* import io.ktor.server.routing.* fun main(args: Array) { embeddedServer(Netty, port = 8083, host = "127.0.0.1") { routing { route("/api") { val v1 = route("/v1") { get("/1") { call.respondText("1") } get("/2") { call.respondText("2") } } route("/v2") { for (child in (v1 as RoutingNode).children) { copyChildrenRecursively(child) } get("/3") { call.respondText("3") } } } } }.start(wait = true) } private fun Route.copyChildrenRecursively(child: RoutingNode) { val copiedChild = createChild(child.selector) @Suppress("INVISIBLE_REFERENCE") for (handler in child.handlers) { copiedChild.handle(handler) } for (ch in child.children) { copiedChild.copyChildrenRecursively(ch) } }