Android developer as Backend developer: Ktor server on Heroku using Git

Barros
4 min readJul 27, 2022

--

Photo by Roman Synkevych 🇺🇦 on Unsplash

As Android developer we are very lucky, as with Kotlin it is possible to create all kinds of projects so why not create a backend project for our apps?

With Ktor, an asynchronous framework for creating microservices, web applications and more, is possible to create a server application.

Setup

You can setup a Ktor project following the documentation here, there is a generators in IntellJ Idea Ultimate or if you use the Community Edition, you can use the Ktor project generator.

Once the project is set, you can change the host to 127.0.0.1 and run the application

When the project is built you can click directly on the link shown

and you can see the response page

Content negotiation and serialization

We have to add two dependencies and the relative plugins:

  • Content negotiation, serves to negotiate media types between the client and server and serializing and deserializing the content in a specific format.
  • JSON, to serialize and deserialize JSON data, in this case we choose the Kotlin serialization plugin

After we install the ContentNegotiation plugin in the embeddedServer

  • prettyPrint specifies whether resulting JSON should be pretty-printed.
  • isLenient makes parser more liberal to the malformed input. In lenient mode, quoted boolean literals, and unquoted string literals are allowed.

Model

We add a Movie data class, this is the object we will return to in the response

First Call

Now that we a Movie we can try to print a list of movies calling 127.0.0.1:8080/movie , adding a GET /movie .

In the routing we add a new GET that returns a list of Movie

After built and navigate to 127.0.0.1:8080/movie the list of Movie JSON is shown:

Deploy on Heroku

I summarize here the steps to deploy, which you can find on the official documentation.

Before make sure that the following prerequisites are met:

  • You have a Heroku account.
  • Heroku CLI is installed on your machine.

First you need to specify a port used to listen for incoming requests, we remove the localhost and change the parameter in embeddedServer

To make the project executable by Heroku we put a task in build.gradle.kts

Also, in the root project we create a Procfile it specifies a path to the application’s executable and allows Heroku to start the application.

To deploy the application using Git in the terminal we have to commit all our implementations and after login Heroku CLI:

heroku login

Create our application:

heroku create ktor-movies

And push the changes:

git push heroku main

Wait until Heroku publishes the application:

remote:  https://ktor-movies.herokuapp.com/ deployed to Heroku

Now opening the URL with the GET path you can see the JSON with the movie list:

In the following repository, you can find the completed runnable implementation:

Thank you, I hope you enjoyed reading it.

--

--

Responses (1)