https://kotlinlang.org logo
#ktor
Title
# ktor
j

johnaqel

06/30/2021, 12:34 AM
Hi folks. Let’s say I have a string representing a path “/some/path”. I’d like to know if a route has been defined for that path. Is there a way to search and see if a route matching that path has already been defined?
c

crummy

06/30/2021, 1:26 AM
a

Aleksei Tirman [JB]

06/30/2021, 11:49 AM
You can subscribe to an application startup event and there, probe the resolution of a specific request by the routing. In order to run the following example, you need
implementation("io.ktor:ktor-server-test-host:$ktor_version")
dependency:
Copy code
embeddedServer(Netty, port = 9090) {
    environment.monitor.subscribe(ApplicationStarted) { app ->
        val routing = app.feature(Routing)
        val call = TestApplicationCall(app, coroutineContext = EmptyCoroutineContext)
        // The request we want to probe
        call.request.method = HttpMethod.Get
        call.request.uri = "/some"

        val context = RoutingResolveContext(routing, call, emptyList())
        if (context.resolve() is RoutingResolveResult.Success) {
            println("Does exist")
        }
    }

    routing {
        get("/some") {
            call.respondText { "Hello" }
        }
    }
}.start(wait = true)
j

johnaqel

06/30/2021, 10:10 PM
Thanks @crummy and @Aleksei Tirman [JB] for the helpful answers.