Kotor是一个框架用于在Kotlin中快速创建Web应用

网友投稿 1015 2022-10-31

Kotor是一个框架用于在Kotlin中快速创建Web应用

Kotor是一个框架用于在Kotlin中快速创建Web应用

Ktor is a framework for quickly creating web applications in Kotlin with minimal effort.

embeddedNettyServer(8080) { get("/") { call.respondText(ContentType.Text.Html, "Hello, world!") }}.start(wait = true)

Runs embedded web server on localhost:8080Installs routing and responds with Hello, world! when receiving GET http request for root path

Documentation

Please visit our Documentation Wiki for quick start, detailed explanations of features, usage and machinery.

Principles

Unopinionated

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

Ktor Application 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, though admittedly it's not an easy task.

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

Asynchronous

Ktor pipeline machinery and API is utilising a number of Kotlin features to provide easy-to-use asynchronous programming model without making it too cumbersome.

Testable

Ktor application can be hosted in a TestHost, which emulates to some extent web server 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. Integration tests with real embedded web server are of course possible, too.

Features

In addition to core HTTP request processing and response building, Ktor provides a number of features out of the box, all implemented through its extensibility:

Routing: attaches code to specific path/query/method/header and extract parameters from placeholdersSessions: stores and retrieves additional information attached to client sessionContent transformations: transforms response content on the fly and utilise unified mechanism to build a responseAuthentication: authenticates client using Basic, Digest, Form, OAuth (1a & 2)Custom status pages: sends custom content for specific status responses such as 404 Not FoundContent type mapping: maps file extension to mime type for static file servingTemplate engines: uses content transformation to enable transparent template engine usageStatic content: serves streams of data from local file system with full asynchronous supportHTTP core features Compression: enables gzip/deflate compression when client accepts itConditional Headers: sends 304 Not Modified response when if-modified-since/etag indicate content is the samePartial Content: sends partial content for streaming ranges, like in video streamsAutomatic HEAD response: responds to HEAD requests by running through pipeline and dropping response bodyCORS: verifies and sends headers according to cross-origin resource sharing controlHSTS and https redirect: supports strict transport security

Maven

Add a repository

false bintray-kotlin-ktor bintray http://dl.bintray.com/kotlin/ktor

Add a dependency:

org.jetbrains.ktor ktor-core LATEST

Inspirations

Wasabi, Kara

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:opencv-python教程学习系列10-颜色空间转换
下一篇:git log 退出方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~