A collection of ready-to-use samples for Ktor.
"Hello World" application with various deployment scenarios:
- netty — Netty engine application.
- netty-embedded — Netty engine embedded application.
- jetty — Jetty engine application.
- jetty-embedded — Jetty engine embedded application.
- jetty-war — Jetty application server WAR deployment.
- tomcat-war — Tomcat application server WAR deployment.
- google-appengine-standard — Google App Engine Standard (war deployment).
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
- locations — experimental 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.htmlfor rendering and experimental locations feature.
Each sample can be run with
Then navigate to http://localhost:8080/ to see the sample home page.
Some samples require additional setup as explained in their readme files.
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:
srcdirectory contains sources directly (no
src/main/kotlinand package directories).
resourcesdirectory contains resources.
WEB-INFdirectory for samples that are deployed as WARs.