Skip to content

Ktor DSL Overview

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

Ktor DSL is a special engine for Ktor developed by Kotless to work in Serverless runtimes.

Basically, this DSL gives you access to all Ktor features. Note that features that require the storing of objects in static variables (like default authorization) are not currently supported and may work incorrectly.

All the dynamic and static Ktor routes should be compile-time constants, since during compile-time deployment is generated by Kotless. We are working on alleviating this problem.

Ktor DSL includes:

  • Dynamic and static routing by Ktor standard DSL;
  • All other features of Ktor;
  • Lifecycle control — LambdaWarming event for warming, default Ktor ApplicationStart for the initialization of application;
  • Permissions granting — annotations to grant permissions to resources in a declarative way.

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

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() }
                }
            }
        }
        
        app.events {
            subscribe(LambdaWarming) {
                println("Lambda warming execution")
            }
        }
    }
}