I'm new to Kotlin coming from the Python world, and wanted to get into Web Development with Kotlin with Ktor.
Now I've started the tutorial https://ktor.io/docs/creating-interactive-website.html#running-our-application-for-the-first-time and already I don't understand quite a few things.
Looking at the code for Application.kt, which is
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")
}
}
}
I don't understand what the syntax means. I'm assuming module is an extension function for the class Application? But what does routing and static mean. Can someone explain the concepts happening there, or maybe just name them so I can google them.
Thanks in advance :-)
Edit: For clarification. I know where to look for when it comes to routing concepts and such for ktor. What I am asking is what routing is. Is it a function? But if so what parameters does it take, it does not look like a normal function definition and it is declared inside another function. When I look at the types it seems it takes a lambda function with type Routing.() -> Unit. What does Routing.() mean... That's what I don't understand.
CodePudding user response:
The routing
is an extension function of the Application
that installs the Routing
plugin and configures it using a provided lambda. The Route.() -> Unit
is a function type with the Route
receiver (this
has type Route
inside this function) that has no parameters and that returns Unit
(nothing). For more information please read the official Kotlin documentation. So it's the build pattern in action, that allows writing DSLs like Ktor's routing.