The multiple connectors example in the docs has: `...
# ktor
d
The multiple connectors example in the docs has:
Copy code
fun Application.main() {
    routing {
        get("/") {
            if (call.request.local.port == 8080) {
                call.respondText("Connected to public api")
            } else {
                call.respondText("Connected to private api")
            }
        }
    }
}
I think no one would want to handle internal/external apis in this way... is there a way to do:
Copy code
fun Application.main() {
    routing {
      port(8080) {
        get("/") {
                call.respondText("Connected to public api")
        }
     }
     port(9090) {
        get("/") {
                call.respondText("Connected to private api")
        }
     }
    }
}
?
d
I guess you can create a custom selector for that
What about?
Copy code
kotlin

data class HttpPortRouteSelector(val port: Int) : RouteSelector(RouteSelectorEvaluation.qualityParameter) {
    override fun evaluate(context: RoutingResolveContext, segmentIndex: Int): RouteSelectorEvaluation {
        if (context.call.request.local.port == port)
            return RouteSelectorEvaluation.Constant
        return RouteSelectorEvaluation.Failed
    }

    override fun toString(): String = "(port:$port)"
}

@ContextDsl
fun Route.port(path: String, port: Int, build: Route.() -> Unit): Route {
    val selector = HttpPortRouteSelector(port)
    return createRouteFromPath(path).createChild(selector).apply(build)
}

@ContextDsl
fun Route.port(port: Int, build: Route.() -> Unit): Route {
    val selector = HttpPortRouteSelector(port)
    return createChild(selector).apply(build)
}
Untested, but I bet it should work
d
Thanks, I'll try!
👍 1