Ktor Framework

repository·main·Indexed 12 days ago

https://github.com/ktorio/ktor

An asynchronous framework written in Kotlin for creating microservices and web applications. Built on Kotlin coroutines, Ktor is unopinionated and designed for high testability, supporting standalone hosting via Netty or Jetty, as well as servlet containers. It includes a Gradle plugin (version 3.1.1) for BOM management and deployment, and provides specialized plugins like ResponseObserver and SSE for client-side operations.

Tokens
3.4K
Snippets
19
Records
25
Agent score
96%

What's inside Ktor

  1. How Ktor works: Core Principles

    main

    Ktor is designed around three main pillars:

    1. Unopinionated: Ktor does not force specific technologies for logging, persistence, or serialization. It uses a unified interception mechanism to install features into an application, allowing you to build arbitrary pipelines. It can be hosted in servlet containers (like Tomcat) or standalone (using Netty or Jetty).
    2. Asynchronous: The framework and its API are built on Kotlin coroutines, providing an asynchronous programming model. All host implementations use asynchronous I/O to prevent thread blocking.
    3. Testable: Ktor provides a special test environment that emulates a web server without actual networking. This allows for high-performance integration testing without heavy mocking.
  2. Disable Develocity build scans and remote cache

    main
    If you are not a JetBrains developer and want to prevent Gradle from attempting to publish build scans or access the remote build cache, add the ktor.develocity.skipBuildScans=true property to your ~/.gradle/gradle.properties file.
    ktor.develocity.skipBuildScans=true
  3. Provision Develocity access keys

    main

    To enable build scans and remote build cache features, you must first authenticate. Run the :provisionDevelocityAccessKey task via Gradle to log in using your Google work account. Once provisioned, build scans will be automatically published after each build, and Gradle will utilize the remote build caches to accelerate local builds.

    ./gradlew :provisionDevelocityAccessKey
  4. Include build-logic in your project using composite builds

    main

    The build-logic project contains shared build logic for Ktor subprojects. Instead of using buildSrc, it uses Gradle composite builds to ensure that changes to the build logic do not cause the entire project to become out-of-date.

    To use this shared logic in your project, you must include the build-logic directory as a composite build in your root settings.gradle.kts file.

    // <root project dir>/settings.gradle.kts
    includeBuild("build-logic")
  5. Apply the ktorbuild.base plugin

    main

    Once build-logic is included via includeBuild in your settings.gradle.kts, you can apply the shared Ktor build configuration to your root build.gradle.kts using the ktorbuild.base plugin ID.

    // <root project dir>/build.gradle.kts
    plugins {
        id("ktorbuild.base")
    }
  6. Replace deprecated ContentNegotiation and Feature management in Ktor 1.6.x

    main

    The following features and management functions are deprecated in Ktor 1.6.x:

    • ContentNegotiation constructor: Do not call the constructor explicitly. Instead, pass configuration options during the installation of the ContentNegotiation plugin.
    • Feature Uninstallation: The functions uninstallAllFeatures, uninstall, and uninstallFeature are deprecated. There are currently no direct replacements for these functions in 1.6.x.
  7. Migrate authentication providers in Ktor 1.6.x

    main

    When using basic or digest authentication providers in Ktor 1.6.x, several properties have been deprecated in favor of more explicit functions and data classes:

    • Use the sendWithoutRequest() function instead of the sendWithoutRequest property.
    • Use the credentials() function instead of the username and password properties. The credentials() function accepts BasicAuthCredentials or DigestAuthCredentials data classes.
  8. Replace deprecated ByteChannel and Buffer APIs in Ktor 1.6.x

    main

    For low-level byte operations in Ktor 1.6.x, use the following replacements:

    • Byte Order: ByteChannelSequentialBase.readByteOrder and ByteChannelSequentialBase.writeByOrder are deprecated. Read/write using big endian, and if necessary, call the ByteChannelSequentialBase.reverseByteOrder() extension function.
    • Buffers: IoBuffer is deprecated. Use ChunkBuffer instead.
    • Input/Output: AbstractInput and AbstractOutput are deprecated and will be merged with Input and Output respectively in version 2.0.0.
  9. Install Ktor in your project

    main

    To use Ktor, add the necessary dependencies to your build configuration. You can use Maven Central as your repository. For a standard Netty server implementation, add io.ktor:ktor-server-netty to your dependencies.

    repositories {
        mavenCentral()
    }
    
    dependencies {
        implementation("io.ktor:ktor-server-netty:$ktor_version")
    }
  10. Use the Ktor Gradle Plugin

    main

    The Ktor Gradle Plugin can be used to manage the Bill of Materials (BOM), run specific tasks, and handle deployment. When using the plugin, you can omit the version in your dependency declarations as the plugin manages the compatible versions.

    plugins {
        id("io.ktor.plugin") version "3.1.1"
    }
    
    dependencies {
        implementation("io.ktor:ktor-server-netty")
    }
  11. Replace deprecated ApplicationCall and TestApplicationCall APIs in Ktor 1.6.x

    main

    The following APIs in Ktor 1.6.x are deprecated and should be replaced to ensure compatibility with future versions:

    • TestApplicationCall.requestHandled: Instead of checking if a request was handled, validate the specific status, header, or content of the request depending on your test case.
    • ApplicationCall.locationOrNull: Use ApplicationCall.location instead.