在ktor中,看来是通过拦截器来进行自定义权限检查的方法,就像这样:
route("/portal") {
route("articles") { … }
route("admin") {
intercept(ApplicationCallPipeline.Features) { … } // verify admin privileges
route("article/{id}") { … } // manage article with {id}
route("profile/{id}") { … } // manage profile with {id}
}
}
提取拦截器逻辑以在代码库中其他位置重用的最佳方法是什么?
答案 0 :(得分:1)
很抱歉迟到了。在我的代码中,我创建了路由,有些路由具有拦截器来测量和记录执行时间,而其他路由则没有。因此,我已经按照文档(https://ktor.io/advanced/pipeline/route.html#)中的示例创建了一个函数,然后使用此功能,但该函数围绕需要测量的一组路由。
请在下面找到我的代码
install(Routing) {
val konfig = HoconKonfigAdapter()
val contextPath = konfig.get("ktor.deployment.context-path")
route("$contextPath/api/v1") {
val registry = feature(Metrics).registry
healthEndPoints()
metricsEndPoints(registry)
routeWithMeasureTime {
catalogSiEndPoints()
reunionCatalogEditoEndPoints()
telesurveillanceCatalogEditoEndPoints()
catalogLegacyEndPoints()
}
}
}
routeWithMeasureTime块内的所有路由都将被拦截并测量。另一个,不。
希望它能帮助事件这么晚。