Git Product home page Git Product logo

ktor's Introduction

Ktor

Download TeamCity (simple build status) GitHub license

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

import org.jetbrains.ktor.netty.*
import org.jetbrains.ktor.routing.*
import org.jetbrains.ktor.application.*
import org.jetbrains.ktor.host.*
import org.jetbrains.ktor.http.*
import org.jetbrains.ktor.response.*

fun main(args: Array<String>) {
    embeddedServer(Netty, 8080) {
        routing {
            get("/") {
                call.respondText("Hello, world!", ContentType.Text.Html)
            }
        }
    }.start(wait = true)
}
  • Runs embedded web server on localhost:8080
  • Installs 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.

If you want to contribute to Documentation please clone Documentation Repository, edit it and send us a Pull Request. We will sync wiki in this project and that repository periodically as needed.

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 placeholders
  • Sessions: stores and retrieves additional information attached to client session
  • Content transformations: transforms response content on the fly and utilise unified mechanism to build a response
  • Authentication: authenticates client using Basic, Digest, Form, OAuth (1a & 2)
  • Custom status pages: sends custom content for specific status responses such as 404 Not Found
  • Content type mapping: maps file extension to mime type for static file serving
  • Template engines: uses content transformation to enable transparent template engine usage
  • Static content: serves streams of data from local file system with full asynchronous support
  • HTTP core features
    • Compression: enables gzip/deflate compression when client accepts it
    • Conditional Headers: sends 304 Not Modified response when if-modified-since/etag indicate content is the same
    • Partial Content: sends partial content for streaming ranges, like in video streams
    • Automatic HEAD response: responds to HEAD requests by running through pipeline and dropping response body
    • CORS: verifies and sends headers according to cross-origin resource sharing control
    • HSTS and https redirect: supports strict transport security

Maven

Add a repository

<repository>
    <snapshots>
        <enabled>false</enabled>
    </snapshots>
    <id>bintray-kotlin-ktor</id>
    <name>bintray</name>
    <url>https://dl.bintray.com/kotlin/ktor</url>
</repository>

Add a dependency:

<dependency>
    <groupId>org.jetbrains.ktor</groupId>
    <artifactId>ktor-core</artifactId>
    <version>${ktor.version}</version>
</dependency>

<!-- you also may need to include host implementation as well, for example

<dependency>
    <groupId>org.jetbrains.ktor</groupId>
    <artifactId>ktor-jetty</artifactId>
    <version>${ktor.version}</version>
</dependency>

-->

Gradle

repositories {
    jcenter()
    maven { url "https://dl.bintray.com/kotlin/kotlinx" }
    maven { url "https://dl.bintray.com/kotlin/ktor" }
}

dependency:

dependencies {
    compile "org.jetbrains.ktor:ktor-core:$ktorVersion"
    // you may also need to include host implementation as well, for example
    // compile "org.jetbrains.ktor:ktor-jetty:$ktorVersion"
}

Inspirations

Wasabi, Kara

ktor's People

Contributors

andrewoma avatar cy6ergn0m avatar elizarov avatar juliocbcotta avatar olivero2 avatar orangy avatar pabl0rg avatar pw10n avatar shinriyo avatar spand avatar tokuhirom avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.