Skip to content

Ktor HTTP API

Vladislav.Tankov edited this page Jun 13, 2020 · 4 revisions

Ktor DSL uses standard Ktor routing and static feature to define HTTP API.

Note that it is required for those routes to be compile-time constant.

The entrypoint of Ktor application is a class implementing io.kotless.dsl.ktor.Kotless abstract class. You'll just need to override its prepare method with necessary configuration:

Here is a simple definition of Ktor DSL-based application:

class Server : Kotless() {
    override fun prepare(app: Application) {
        app.routing {
            static {
                staticRootFolder = File("src/main/static")

                static("css") {
                    files("css")
                }

                file("favicon.apng")
            }

            get("/") {
                call.respondHtml { main() }
            }

            //Supports route inner calls
            route("pages") {
                get("/introduction") {
                    call.respondHtml { introduction() }
                }

                get("/faq") {
                    call.respondHtml { faq() }
                }
            }
        }
    }
}

Note, that Kotless itself does not extend or change Ktor API — just use it and let Kotless do serverless magic for you.