Application.kt 中 Ktor 的 Kotlin 语法解释
Explanation of Kotlin Syntax for Ktor in Application.kt
我是来自 Python 世界的 Kotlin 新手,想通过 Ktor 和 Kotlin 进行 Web 开发。
现在我已经开始教程了https://ktor.io/docs/creating-interactive-website.html#running-our-application-for-the-first-time,但我已经不明白很多东西了。
查看 Application.kt 的代码,即
package com.jetbrains.handson.website
import freemarker.cache.*
import freemarker.core.HTMLOutputFormat
import io.ktor.application.*
import io.ktor.freemarker.*
import io.ktor.html.respondHtml
import io.ktor.http.HttpStatusCode
import io.ktor.http.content.*
import io.ktor.request.receiveParameters
import io.ktor.response.*
import io.ktor.routing.*
import kotlinx.html.*
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
fun Application.module() {
routing {
static("/static") {
resources("files")
}
}
}
我不明白语法是什么意思。我假设模块是 class 应用程序的扩展功能?但是路由和静态是什么意思。有人可以解释那里发生的概念,或者只是命名它们以便我可以 google 它们。
提前致谢:-)
编辑:澄清一下。当涉及到路由概念等 ktor 时,我知道在哪里寻找。我要问的是路由是什么。它是一个函数吗?但如果是这样的话,它需要什么参数,它看起来不像一个普通的函数定义,它是在另一个函数中声明的。当我查看类型时,它似乎需要一个类型为 Routing.() -> Unit 的 lambda 函数。 Routing.() 是什么意思...
这就是我不明白的。
routing
是 Application
的扩展函数,它安装 Routing
插件并使用提供的 lambda 对其进行配置。 Route.() -> Unit
是一个带有 Route
接收者的函数类型(this
在这个函数中有类型 Route
),没有参数并且 returns Unit
(没有什么)。有关更多信息,请阅读官方 Kotlin documentation. So it's the build pattern in action,它允许像 Ktor 的路由一样编写 DSL。
我是来自 Python 世界的 Kotlin 新手,想通过 Ktor 和 Kotlin 进行 Web 开发。
现在我已经开始教程了https://ktor.io/docs/creating-interactive-website.html#running-our-application-for-the-first-time,但我已经不明白很多东西了。
查看 Application.kt 的代码,即
package com.jetbrains.handson.website
import freemarker.cache.*
import freemarker.core.HTMLOutputFormat
import io.ktor.application.*
import io.ktor.freemarker.*
import io.ktor.html.respondHtml
import io.ktor.http.HttpStatusCode
import io.ktor.http.content.*
import io.ktor.request.receiveParameters
import io.ktor.response.*
import io.ktor.routing.*
import kotlinx.html.*
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
fun Application.module() {
routing {
static("/static") {
resources("files")
}
}
}
我不明白语法是什么意思。我假设模块是 class 应用程序的扩展功能?但是路由和静态是什么意思。有人可以解释那里发生的概念,或者只是命名它们以便我可以 google 它们。
提前致谢:-)
编辑:澄清一下。当涉及到路由概念等 ktor 时,我知道在哪里寻找。我要问的是路由是什么。它是一个函数吗?但如果是这样的话,它需要什么参数,它看起来不像一个普通的函数定义,它是在另一个函数中声明的。当我查看类型时,它似乎需要一个类型为 Routing.() -> Unit 的 lambda 函数。 Routing.() 是什么意思... 这就是我不明白的。
routing
是 Application
的扩展函数,它安装 Routing
插件并使用提供的 lambda 对其进行配置。 Route.() -> Unit
是一个带有 Route
接收者的函数类型(this
在这个函数中有类型 Route
),没有参数并且 returns Unit
(没有什么)。有关更多信息,请阅读官方 Kotlin documentation. So it's the build pattern in action,它允许像 Ktor 的路由一样编写 DSL。