Is there a better way to define a default handler ...
# ktor
s
Is there a better way to define a default handler for requests than matching on a catch-all Regex? I want to log all requests that didn't correspond to the available paths, so currently I use something like
Copy code
routing {
        ...
        route(".*".toRegex()) {
            handle {
                <http://log.info|log.info>("request on unsupported path "+call.request.path())
                call.respond(HttpStatusCode.NotFound)
            }
        }
    }
but the log output that matching ".*".toRegex() creates is kind of ugly, so I wonder if there's something equivalent like
Copy code
routing {
        ...
        defaultRoute {
            handle {
                <http://log.info|log.info>("request on unsupported path "+call.request.path())
                call.respond(HttpStatusCode.NotFound)
            }
        }
    }
?
1
a
You can use the tailcard path pattern.
s
yes, that works! awesome 👍
i've build myself a helper function
Copy code
inline fun Route.defaultRoute(crossinline handler: Route.()->Unit): Route = this.route("{...}") {
    handler()
}
so that the routing config itself looks exactly as I envisioned 🤓