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)

Searching Database…

14.5K ·GitHub
这是做什么的?Ktor 是一个异步框架,用于创建微服务、Web 应用等,从头到尾都用 Kotlin 编写。
它是框架还是库?是一个完整的框架:你创建一个 Application,然后往里面安装各种功能(feature),这些功能通过统一的拦截机制接入,可以搭建任意的处理管道。
我怎么把第一个应用跑起来?添加依赖,创建一个 Application 并安装路由之类的功能,然后运行——一个内嵌的 Web 服务器就会在 localhost:8080 上启动。start.ktor.io 还能帮你生成一个初始项目。
我的电脑上需要装什么才能用它?一个用 Gradle 或 Maven 构建的 Kotlin 项目;两者都有入门指南,IntelliJ IDEA 也有。可选的 Ktor Gradle Plugin 可以帮你配置 BOM、运行任务和部署。
Ktor 会不会强制我使用特定的日志、序列化或模板方案?不会——它刻意保持中立。它对日志、模板、消息、持久化、序列化或依赖注入都不做任何限制;通常只要写一个转换或拦截函数就可以了。
Ktor 的代码长什么样?大部分是带 lambda 的函数调用——Kotlin 的 DSL 能力让代码看起来像声明式的一样。应用的组装方式由你决定:可以用函数或类,可以用 DI 框架,也可以全都写在 main() 里。
怎么测试一个 Ktor 应用?可以把它托管在一个特殊的测试环境里,它模拟了 Web 服务器但不走真实网络,这样你几乎不用打桩(mock)就能验证调用,性能也很好。对着真实内嵌服务器跑集成测试也行。
这是 JetBrains 的官方项目吗?是的——Ktor 是 JetBrains 的官方产品,主要由 JetBrains 团队开发,也有社区贡献。它的灵感来自已经停止维护的 Kotlin Web 框架 Wasabi 和 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