Rob Elliot
03/09/2023, 4:55 PMdave
03/09/2023, 4:59 PMRob Elliot
03/09/2023, 5:02 PMhfhbd
03/09/2023, 7:26 PMs4nchez
03/09/2023, 8:24 PMdave
03/09/2023, 8:26 PMRob Elliot
03/10/2023, 10:07 PMDominik Sandjaja
03/13/2023, 8:27 AM// Find duplicate routes and don't start if there are any.
// The logic tries to only look at the relevant aspects, i.e. only the actual path and HTTP method.
fun Application.avoidDuplicateRoutes() {
routing {
var rootRoute: Route = children.first()
while (rootRoute.parent != null) {
rootRoute = rootRoute.parent!!
}
allRoutes(rootRoute).filter { it.selector is HttpMethodRouteSelector }
.map { it.toString() }
.map { normalize(it) }
.sorted()
.onEach { <http://logger.info|logger.info>("Found route $it") }
.groupingBy { it }
.eachCount()
.filter { it.value > 1 }
.forEach { (route, _) ->
throw IllegalStateException("There are multiple routes for the path $route")
}
}
}
// remove all authentication and authorization routes, we are only interested in the actual paths
private fun normalize(routeString: String): String {
return routeString.replace(Regex("\\/\\(authenticate[ a-zA-Z,]*\\)"), "")
.replace(Regex("\\/\\(authorize[ a-zA-Z_,]*\\)"), "")
.replace(Regex("\\{[\\w\\?]+\\}"), "{}")
}
private fun allRoutes(root: Route): List<Route> {
return listOf(root) + root.children.flatMap { allRoutes(it) }
}
dave
03/13/2023, 8:37 AMDominik Sandjaja
03/13/2023, 8:39 AM