Ktor
Building Asynchronous Servers and Clients in Kotlin
Ktor 2.1.0 Released and It Comes With Goodies!
We’ve just released Ktor 2.1.0, and in addition to the new features and bug fixes, we have a few new things we’d love for you to try out. In particular, we are releasing betas of three new tools:
- Command Line tool
- Yeoman Generator
- Gradle Deployment Plugin
- YAML configuration support
Let’s take a look at each of these individually.
Native Command Line Tool
Ktor provides two ways to simplify creating new application templates – IntelliJ IDEA or start.ktor.io.
We’ve now extended this by providing a command line tool built in Kotlin/Native. However, beyond just generating a Ktor server application for you, it also takes care of downloading a JDK if your system does not have one installed. This is ideal for folks who are new to the JVM.
To create a new project, simply type:
ktor generate {projectName}
Once completed (which will also include downloading JDK/Gradle if necessary), it will build the project.
All that’s left to do is run it!
The tool is currently available for macOS and Linux. Windows support will come later on.
For macOS you can download it directly from the release page or install it via brew with:
brew tap ktorio/ktor
brew install --build-from-source ktor
On Linux we’re working on trying to get it published to Snap. In the meantime you can use the release page.
The command line tool does not currently provide the option to customize your project, but the next thing we’re going to show you does!
Yeoman Generator
Similar to the command line client, we’ve also added Yeoman support. Yeoman is a command line tool that allows you to easily generate scaffolding for a variety of projects. Ktor is now one of these!
If you do not have Yeoman installed, make sure you first install node/npm (compatible with versions above 14.0.0). Once you have that, you can run:
npm install -g yo
This installs Yeoman globally on your system. Next step is to install the Ktor generators:
npm install -g generator-ktor
Once installed, you can simply create a new project directory and run the generator using:
yo ktor
It will prompt you for a series of inputs:
Then it will generate the project. This time it will automatically run it for you, too!
Gradle Deployment Plugin
One of our goals with Ktor is to make the overall development experience as smooth and enjoyable as possible. Of course, people rarely develop an application and do not deploy it.
To this end, we’ve recently released a beta of our new Gradle plugin for deployment (note that newly created projects using the wizards will add this plugin by default). You can now define how you would like your Ktor application to be deployed in a Gradle file:
ktor { docker { jreVersion.set(JreVersion.JRE_17) localImageName.set("sample-docker-image") imageTag.set("0.0.1-preview") externalRegistry.set( DockerImageRegistry.dockerHub( appName = provider { "ktor-app" }, username = providers.environmentVariable("DOCKER_HUB_USERNAME"), password = providers.environmentVariable("DOCKER_HUB_PASSWORD") ) ) } }
The plugin provides several new tasks allowing you to build, run, and publish Docker images, as well as work with FatJars, with GraalVM native images in the works.
ktor { fatJar { archiveFileName.set("fat.jar") } }
While it is currently only available for Gradle, the plan is to provide Maven support as well. For information about the list of tasks offered, check out the notes on GitHub. You can also find more details in the Ktor documentation, both about fat jars and about Docker.
YAML configuration support
In addition to configuring Ktor applications using code or HOCON, you can now use YAML, which is also available for Ktor native server applications. Take the following HOCON configuration:
ktor { deployment { port = 8080 } application { modules = [ com.example.ApplicationKt.module ] } }
In YAML, the equivalent would be:
ktor: deployment: port: 8080 application: modules: - com.example.ApplicationKt.module
You can find out more about YAML configuration in the documentation.
We’d love for you to try these new tools out and give us some feedback!