/ktor-samples

Sample projects for Ktor

Primary LanguageKotlinApache License 2.0Apache-2.0

Samples for Ktor

A collection of ready-to-use samples for Ktor.

  • "Hello World" application with various deployment scenarios:

  • Other build systems and other samples:

    • maven-netty — Maven build for Netty engine embedded application.
    • maven-google-appengine-standard — Maven build for Google App Engine Standard (war deployment).
    • proguard — Embedded application minimized with ProGuard.
    • multiple-connectors — Embedded application listening to several endpoints.
    • sandbox — Application including all Ktor artifacts, suitable for Scratches files and quick experiments.
    • simulate-slow-server — Application showing how to simulate delay in the whole application creating a simple interceptor.
    • rx — Application showing how to use RxJava2.
    • css-dsl — Application showing how to combine HTML DSL with CSS DSL.
    • fullstack-mpp — Application showing how to combine Ktor serving Kotlin.JS static scripts.
    • filelisting — Application showing how to create a file listing for static files.
    • structured-logging — Application showing how to use logstash-logback-encoder for structured logging with scoped variables without requiring MDC ThreadStatic.
    • client-multipart — Application showing how to do multipart requests with the HttpClient.
    • client-tools — Application showing several useful extension methods not included in Ktor itself.
    • sse — Shows how to use SSE (Server-Sent Events) using Ktor.
  • Small single-feature samples:

    • async — long-running asynchronous computation that happens in a separate thread-pool context.
    • auth — using authorization.
    • post — form post and multipart file upload.
    • sessions — store information that will be kept between requests.
    • custom-feature — implementation of a custom feature.
    • html-widget — custom html widget.
    • Content Negotiation
    • Dependency Injection
    • locationsexperimental locations feature.
    • metrics — metrics feature.
    • http2-push — HTTP/2 with server-side push.
    • ssl — SSL support.
    • static content — serving static content.
    • testable — application writing tests for Ktor applications.
    • websocket (tba)
  • Relatively big sample applications:

    • chat — simple chat application using websockets and sessions.
    • httpbin — application implementing (large parts of) HttpBin(1) HTTP Request & Response Service.
    • kweet — messaging application using freemarker templates and experimental locations feature.
    • youkube — video upload/view application using kotlinx.html for rendering and experimental locations feature.

Running samples

Each sample can be run with

./gradlew :<sample-name>:run

Then navigate to http://localhost:8080/ to see the sample home page.

Some samples require additional setup as explained in their readme files.

Cut-and-pasting samples

Each sample is a standalone Gradle project that can be cut-and-pasted to get started with your own project. Cut-and-paste the directory of the corresponding sample together with its build scripts and add gradle.properties from the root that declares Kotlin, Ktor, and other versions. You may remove reference to the versions that this particular sample does not use.

Samples with other build systems (Maven) are fully standalone and can be cut-and-pasted to get started.

Compact directory layout

Samples use compact directory layout whenever possible for ease of navigation:

  • src directory contains sources directly (no src/main/kotlin and package directories).
  • resources directory contains resources.
  • webapp directory contains WEB-INF directory for samples that are deployed as WARs.