【发布时间】:2018-08-01 20:30:34
【问题描述】:
我的问题是关于 Ktor 中路由功能背后的总体情况;在设计具有大量路由的 API 时具有可扩展性。
如果我创建这样的应用程序:
import io.ktor.application.*
import io.ktor.response.*
import io.ktor.request.*
import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty
fun Application.routingExample() {
intercept(ApplicationCallPipeline.Call){
if (call.request.uri == "/") call.respondText("User call for /")
}
}
fun main(args: Array<String>) {
embeddedServer(Netty, 8080, watchPaths = listOf("Routing"), module = Application::routingExample).start()
}
如果我有一个路由数量较少的 api/应用程序,这没关系。但是,我应该以哪种风格扩展这种方法 大量路线(例如,30 条路线和控制器功能)。
我会有很多选择:
大型路由函数:我将有一个大型 Application.routingExample 函数来保存所有路由,因此我不需要更新 main。
大型主函数:会有一个大型函数来保存对不同较小函数的调用;但它会是重复的;至于我想要的 API 在同一个港口为他们服务。
所以我的问题是关于编码风格:有没有办法分解路由控制器关系?
【问题讨论】: