Kotlin 如何在ktor中创建可重用的拦截器?
在ktor中,进行自定义权限检查的方法似乎是通过拦截器,如下所示:Kotlin 如何在ktor中创建可重用的拦截器?,kotlin,routing,ktor,Kotlin,Routing,Ktor,在ktor中,进行自定义权限检查的方法似乎是通过拦截器,如下所示: route("/portal") { route("articles") { … } route("admin") { intercept(ApplicationCallPipeline.Features) { … } // verify admin privileges route("article/{id}") { … } // manage article with {id}
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}
}
}
提取拦截器逻辑以便在代码库中其他地方重用的最佳方法是什么?很抱歉迟到了。在我的代码中,我创建了一些路由,一些路由有一个拦截器来测量和记录执行时间,而其他路由则没有。因此,我根据文档()中的示例创建了一个函数来实现这一点,然后我在需要测量的路由块周围创建了这个函数 请在下面找到我的代码
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()
}
}
}
将拦截和测量闭塞线路内所有线路的测量时间。另一个,不是
希望它能对这么晚的活动有所帮助