Exploring server-side Kotlin using Ktor

Nikhil Jadhav
Mobile Studio India
4 min readApr 14, 2020

It its I/O 2017 developers conference May 17, Google announced Android is gaining official support for the Kotlin programming language, in addition to Java and C++. Google describes Kotlin, which is an open-sourced project under the Apache 2.0 license, as “a brilliantly designed, mature language that we believe will make Android development faster and more fun.”

I am primarily an Android application developer & thus the announcement changed a lot for me. However, In my experience, it turned out way cool to learn, especially for the people from compiled language background, as they can start writing code in Kotlin in no time. Also, we recently started experimenting Kotlin for building projects other than Android. We created an image-search web-app using Kotlin entirely in no time.

We have a separate team for building APIs to be consumed by mobile + web platforms. That gave me an idea to experiment Kotlin for building APIs. So, the following are the findings-

The IDE will be IntelliJ built by Jetbrains. We will also be using Ktor framework.

Ktor is an asynchronous web framework written in and designed for Kotlin. It gives us the ability to create client and server-side applications that can run and target multiple platforms while making use of the impressive features of Kotlin such as Coroutines.

You will have to install plugin start.ktor.io to create a Ktor project. On installation, you will be able to see a separate entry in the ‘Create New Project’ menu.

Without further ado, let's jump directly to creating APIs. This time we will work with the default settings.

Let’s finalise the project requirement first :D.

  • It should have some in-memory database with pre-populated values
  • There should be API for fetching all records
  • There should be API for either create/update/delete of records in-memory database.

Declaring Dependencies

The first step will be adding dependencies in the build.gradle file. Following are the extra additions that I did that were required for the functionality.

Defining Model

The first dependency Exposed — Kotlin SQL Framework which we will be using for creating an in-memory database.

The Ktor plugin creates basic files. Let’s define a table to work within our project.

As you can see, now we have defined a table called “user” with 3 columns.

Initialising Database and Operations

Now we need to initialise the database and prefill some rows in the User table. Following is the code snippet that we will be using to init and prefill the User table.

The above code will create an in-memory DB with table named User and will pre-fill some data so that we can work on creating some APIs on the table.

Let's check the code-

The above code snippet runs SELECT query on the database and returns the result in JSON formatted manner by making use of GSON library.

The above code snippet is for DELETE operation on the in-memory DB that we have. Now, let's jump into code which defines API using which we will be able to make changes to the database.

Defining Routes

As we know that internally all Kotlin code compiles to JVM code. So, it will definitely have the main function which kickstarts the application. Here, in the above snippet, we have the main function. It first does the initialising of DB & starts a server using Netty client-server framework on port 8080. It also defines various routes exposed to be consumed by different platforms. Here, I have defined 3 routes

  • First one: the default and was used for debugging. It returned text “Hello” when hit.
  • Second one: This API makes use of the function to get all records from DB and return it in JSON format.
  • Third one: This API accepted a parameter which was passed to the function. The function deleted any records in table User which matched the value of field name with the given parameter. It also returned text “true” or “false” depending on if the record was deleted or not.

That’s it, people. We will require to just run the application and we will be able to access the APIs (in this case we can test in browser as we have defined GET methods).

What motivated me to share my experience

I always thought API implementation a tedious process which involved a lot of steps. I always assumed creating REST APIs took a lot of time as it has a lot of things to be taken care of before one can start consuming the APIs. With Kotlin and well-built framework — Ktor implementing server is now as easy as writing a function.

To be fair, it just took some reading and learning to accomplish the POC in a few hours. That includes exploration and implementation (& writing this blog as well :P). You will be able to accomplish more and utilise time efficiently.

Also, Ktor is built by Jetbrains aka the guys who built Kotlin & also the powerful IDE — IntelliJ. What we learned was just a tiny little part from the huge functionality that Ktor provides. Happy Coding :)

#BeKindKotlinChallenge

Inspired by Kotlin, we’ve launched a coding contest powered by Globant and Jetbrains.

Click here to know more!

Team Credits: Mukund | Sumit Sutar| Akshata Shelar | Archana

--

--