Bring Kotlin to cloud — a hobby project to explore the possibility

Toan Tran
Toan Tran
Nov 30, 2019 · 7 min read

In this article, I will document my journey of bringing Kotlin language to power a Backend application that ultimately helps me to unify the tech-stack I’d to use across the project.

Photo courtesy by Mitchell Luo

Bringing Kotlin to the cloud — my project to explore the possibilities — Part 1

With my background in Android development, how could I not be a fan of Kotlin lang? Moving from Java (which I am still using for any legacy codebase I encounter) to Kotlin is something that has helped me to really love what I do, to put me in a better mood as I work.


Part 1 — What I build and primary setup

The app — Calories Intake tracker app — will help me to know how much food/energy I have consumed during the day and let me know if I have reached my goal.

(Disclosure: I’m in the process of gaining weight)

This will be how the app should look like

Now for the interesting part! In the Backend, I would like for my app to store my historical data, and for this data to be accessible anywhere with an internet connection. I’d like to make it simple by building a Restful API Backend with the following CRUD operations:

  1. Create a new log entry for food intake
  2. Read all the entries I have
  3. Update a certain log entry
  4. Delete log entry

Time to explore

A Google search of “kotlin google cloud” brings back ‘https://cloud.google.com/kotlin/’ as the top result.

Time to pick what I need

So what I need from the “Backend” in terms of engineering here is:

  1. It must be somewhat familiar to me (i.e. the language, the concept)
  2. It must be quick to create a prototype, with super-easy deployment (I’m not going to spend weeks learning how to configure the server properly)
  3. It must auto-scale — Just in case my app is going to hit the market hard!! 💵 💵 💵 💵 💵 💵 💵 💵 💵 💵 💵 💵 💵 💵 💵 💵 💵 💵 💵 💵 💵 💵 💵 💵 💵 💵

I picked:

All good, we are almost done!

Google provides a tutorial on how to proceed on the Ktor + App Engine https://cloud.google.com/community/tutorials/kotlin-ktor-app-engine-java8

  1. Create a project that will host your Ktor application. You can also reuse an existing project.
  2. Use the Google Cloud Platform Console to create a new Cloud Platform project. Remember the project ID; you will need it later. Later, commands in this tutorial will use as a substitution, so you might consider setting the environment variable in your shell.
  3. Enable billing for your project.
  4. Install the Google Cloud SDK. Make sure you initialize the SDK and set the default project to the new project you created.
  5. Version 175.0.0 or later of the SDK is required.
  6. Install JDK 8 or higher if you do not already have it.

In step 2, I re-used one of the projects on the Google Cloud Platform Console I created when trying out Google Functions (https://medium.com/@toantran/trying-out-function-as-a-service-with-azure-function-ca7b86b74e4f)

The next step, for me, was to explore the sample that GoogleCloudPlatform Github provided at https://github.com/GoogleCloudPlatform/kotlin-samples

And specifically, pay attention to this part — ktor sample inside AppEngine folder
Run the sample to see what I got there

Now that the configuration is set up and running on my local machine, that’s it for Part1! We’ll talk more about the business logic of our application and the deployment to Production with Google Cloud in Part 2.

Bringing Kotlin to the cloud — my personal project to explore the possibilities — Part 2

Part 2 will continue to discuss the business implementation (after Part 1 has already discussed the setup). You can find part 1 above.


Deployment

As I mentioned in Part 1, we are looking for a very easy way to set up and deploy. Let’s verify this first.

I’m checking for

All good. Also, checking that my PROJECT_ID is set as default.

gcloud config set project my-project-id

And, deploy!

It’s verified — one command deployment and my app is now live for the public.

Using gcloud app logs tail -s default I can check the logstream from my app on App Engine.

Business implementation

This would be the routing for my web application

The placeholder for my request only.

A get(“entries”) will return all the entries of food intake available

A post(“entries”) will send an array of food intake entries to our server to store in DataStore.

But there’s a problem! I want to check what will the request body of my “post” to our server look like? println()? no, thanks! Let’s do it the formal way, by having the debugger help here.

When I check on the quickstart page of Ktor, it seems there is something I missed here.

I’ll need to add ktor netty plugin to be able to run the web app inside IntelliJ and then I can debug! What’s next? Adding a new Run Configuration to invoke the app.

And I can read the json body of my POST request in debug mode

Read the json body of POST(“entries”) in debug mode

Next, save them to the local datastore

We need a data model to represent what fields we will capture and store in our database.

and the data repository to help us save/retrieve data in general.

Let’s connect them together.

And run the app with (because it won’t work in IntelliJ without a correct AppEngine project template — I’ll fix this later!)
Trigger the POST and get the result. Check out at http://localhost:8080/_ah/admin/

It works on my machine!

It works but something seems wrong with the encoding when I try to create the content in Vietnamese. A quick search showed this https://github.com/ktorio/ktor/issues/384 The issue is still open with Ktor and seems to have an impact on the body content of requests only. I found a workaround on this https://github.com/ktorio/ktor/issues/384#issuecomment-458542686

It works! (I’ll revisit this later) — at least I can get my “bún bò” to display correctly now!

It’s good again! but wait! the id of your FoodEntry is a Long field and will be sent from the client. This is not scalable because I will have to keep track by counting for the next entry if I don’t want to accidentally delete/override an existing entry — which will cause a problem with data consistency.

I’ll modify the data model for FoodEntry.

The updated model for FoodEntry

My data now can be scaled well!

And it’s time to deploy 🚀

My “bún bò” data entities are now live on Data Store.

it’s live on production!

Voila! The basis of our app is live! You can find the complete sample here under Github https://github.com/toantran-ea/food-intake-backend.

Conclusion

  1. I should have set the project up with the Google App Engine template to debug the app properly.
  2. I had some hiccups with the Ktor framework (related to encoding) but found work-arounds for it in the end.
  3. Short learning curve: As I had some previous experience with Google App Engine, and have used other web frameworks before (ExpressJS/Flask) the concepts were not too alien to me.
  4. The prototype was quick and easy to make, and show live, thanks to the great support of Google Cloud SDK CLI tools.
  5. It was quick to deploy (as you can see!)
  6. Working with Kotlin is very comfortable: As expected, I can use all the Kotlin features I want to without any problem.

Toan Tran

Written by

Toan Tran

AndroidLead @inspectorio. Writing Code to make people’s life better.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade