ktor README

The project's README from github.com/ktorio/ktor. As one markdown file: README.md.

Ktor logo

Official JetBrains project Maven Central Kotlin Slack channel GitHub License Contribute with Gitpod

Ktor is an asynchronous framework for creating microservices, web applications and more. Written in Kotlin from the ground up.

First add the dependency to your project:

repositories {
    mavenCentral()
}

dependencies {
    implementation("io.ktor:ktor-server-netty:$ktor_version")
}

Then create an Application and install some features:

import io.ktor.server.netty.*
import io.ktor.server.routing.*
import io.ktor.server.application.*
import io.ktor.http.*
import io.ktor.server.response.*
import io.ktor.server.engine.*

fun main(args: Array<String>) {
    embeddedServer(Netty, 8080) {
        routing {
            get("/") {
                call.respondText("Hello, world!", ContentType.Text.Html)
            }
        }
    }.start(wait = true)
}

You also can use Ktor Gradle Plugin to configure bom, run tasks and deployment:

plugins {
    id("io.ktor.plugin") version "3.1.1"
}

dependencies {
    implementation("io.ktor:ktor-server-netty")
}

To run the created application, execute:

./gradlew run

Start using Ktor

Build your first Kotlin HTTP or RESTful application using Ktor: start.ktor.io

Principles

Unopinionated

Ktor Framework doesn't impose a lot of constraints on what technology a project is going to use – logging, templating, messaging, persistence, serialization, dependency injection, etc. Sometimes it may be required to implement a simple interface, but usually it is a matter of writing a transforming or intercepting function. Features are installed into the application using a unified interception mechanism which allows building arbitrary pipelines.

Ktor Applications can be hosted in any servlet container with Servlet 3.0+ API support such as Tomcat, or standalone using Netty or Jetty. Support for other hosts can be added through the unified hosting API.

Ktor APIs are mostly functions calls with lambdas. Thanks to Kotlin DSL capabilities, the code looks declarative. Application composition is entirely up to the developer's choice – with functions or classes, using dependency injection framework or doing it all manually in the main function.

Asynchronous

The Ktor pipeline machinery and API are utilising Kotlin coroutines to provide easy-to-use asynchronous programming model without making it too cumbersome. All host implementations are using asynchronous I/O facilities to avoid thread blocking.

Testable

Ktor applications can be hosted in a special test environment, which emulates a web server to some extent without actually doing any networking. It provides easy way to test an application without mocking too much stuff, and still achieve good performance while validating application calls. Running integration tests with a real embedded web server are of course possible, too.

JetBrains Product

Ktor is an official JetBrains product and is primarily developed by the team at JetBrains, with contributions from the community.

Documentation

Please visit ktor.io for Quick Start and detailed explanations of features, usage and machinery.

Reporting Issues / Support

Please use our issue tracker for filing feature requests and bugs. If you'd like to ask a question, we recommend StackOverflow where members of the team monitor frequently.

There is also community support on the Kotlin Slack Ktor channel

Reporting Security Vulnerabilities

If you find a security vulnerability in Ktor, we kindly request that you reach out to the JetBrains security team via our responsible disclosure process.

Inspirations

Kotlin web frameworks such as Wasabi and Kara, which are currently deprecated.

Contributing

Please see the contribution guide and the Code of conduct before contributing.

ktor (ktorio/ktor)

jvm
Searching Database…

14.5K ·GitHub
What is this?Ktor is an asynchronous framework for creating microservices, web applications and more, written in Kotlin from the ground up.
Is it a framework or a library?A full framework: you create an Application and install features into it, which plug in through a unified interception mechanism that builds arbitrary pipelines.
How do I get a first app running?Add the dependency, create an Application and install features like routing, then run it — an embedded web server starts on localhost:8080. start.ktor.io generates a starter project for you.
What do I need on my machine to use it?A Kotlin project built with Gradle or Maven; there are getting-started guides for both plus IntelliJ IDEA. The optional Ktor Gradle Plugin configures the BOM, run tasks and deployment.
Does Ktor force specific choices for logging, serialization or templating?No — it is deliberately unopinionated. It imposes no constraints on logging, templating, messaging, persistence, serialization or dependency injection; usually a transforming or intercepting function is all it takes.
What does Ktor code look like?Mostly function calls with lambdas — Kotlin DSL capabilities make the code look declarative. Application composition is your choice: functions or classes, a DI framework, or everything in main().
How do I test a Ktor application?Host it in a special test environment that emulates a web server without real networking, so you validate calls with little mocking and good performance. Integration tests against a real embedded server also work.
Is this an official JetBrains project?Yes — Ktor is an official JetBrains product, primarily developed by the JetBrains team with community contributions. It draws inspiration from the deprecated Kotlin web frameworks Wasabi and Kara.
What are similar projects?Rocket, express, koa, aiohttp, tornado, axum, starlette, fiber
Creator
ktorio
Size
149.9 MB
Stars
14.5K
Age
11y
Commits · 90 Days