Server-Side Kotlin with Ktor

Learn how to use the Ktor framework to build a Kotlin web app and API, and see how to deploy the app to the cloud and also run the app in a Docker container. By Joe Howard.

Leave a rating/review
Save for later
Comments
Share

Who is this for?

This course is for Kotlin and Android developers interested in learning how to use Kotlin on the server to build a web app and a web API. It expects you to be familiar with the Kotlin language and either Android Studio or IntelliJ IDEA. You'll also need basic knowledge of how HTTP works and have some experience with use of the command-line.

Covered concepts

  • Creating a Ktor web app
  • Building a REST API with Ktor
  • Type-safe routing in a Ktor app
  • HTML Templating with FreeMarker
  • Using Bootstrap with FreeMarker
  • The Exposed SQL Library
  • Using PostgreSQL with Ktor
  • Session and Cookie Authentication
  • JSON Web Token Authentication
  • Deploying a Ktor app to Heroku
  • Dockerizing a Ktor app

Part 1: Getting Started with Ktor

01
Toggle description

Let's introduce Server-Side Kotlin and Ktor, discuss our sample project, and review all that you'll be learning in the course.

Toggle description

You'll create a Ktor app from scratch in IntelliJ IDEA and add simple Hello World routes to the app.

App Routes 1:57
Toggle description

In order to keep your code organized, you'll refactor the routes you've written so far so that each route has its own source file.

Toggle description

Test your knowledge about building app routes in Ktor by adding an about route to the web app, then see a solution.

Toggle description

Add a repository interface and in-memory repository implementation in order to save model EmojiPhrases in between requests to the app.

Toggle description

Create a POST endpoint to which you can send new EmojiPhrases, then test the endpoint using curl and a REST client app.

Get Route 2:00
Toggle description

Add a GET route to the web app that will let you retrieve and see EmojiPhrases that have been add to the repository.

Toggle description

Introduce FreeMarker templating into the app and create a simple HTML page for showing your EmojiPhrases.

Conclusion 0:57
Toggle description

Review all that you've done in Part 1 of the course, and take a look ahead at where we'll be going in Part 2 and beyond.

Part 2: Beautification and Type-Safe Routing

Toggle description

See what we'll be covering in Part 2: adding basic authentication, using Bootstrap components for styling, and adding type-safe routing.

Toggle description

Install the Ktor Authentication feature into your app and add Basic Authentication to the POST route and GET route for phrases.

Toggle description

Build an HTML form to let users post new EmojiPhrases directly from the app instead of using other REST clients.

Toggle description

Get an introduction to the Bootstrap open-source front-end framework, and review the Bootstrap code that has been added to the app FreeMarker templates.

Toggle description

Setup static content in your Ktor web app, and complete the process of adding Bootstrap to the Home and Phrases pages of the app.

Toggle description

Test your knowledge of FreeMarker and the use of Bootstrap by completing the EmojiPhrases app About page, then see a solution.

Toggle description

Install the Ktor Locations feature into the app, then use the feature to switch to using type-safe routing.

Conclusion 1:02
Toggle description

Review all that you've done in Part 2 of the course, and take a look ahead at where we'll be going in Part 3 and beyond.

Part 3: Persistence and Sessions

Toggle description

See what we'll be covering in Part 3 of the course, adding true data persistence using Exposed and PostgreSQL and building in real authentication via sessions.

Toggle description

Add the Exposed SQL library framework from JetBrains into the app, and use it with an in-memory H2 database and with database connection pooling.

PostgreSQL 4:20
Toggle description

Switch from using H2 with Exposed to instead using PostgreSQL, adding true data persistence so that data is saved between different runs of the app.

Toggle description

Learn about user authentication via cookies and sessions, understand the purpose of password hashing, and begin to add user sessions into the course app.

Toggle description

Continue adding sessions into the course app by creating the code for the sign up and sign in pages of the app.

Toggle description

Finish adding sessions into the app by checking for active sessions on the different pages of the app, and verifying a session when users post new data.

Conclusion 0:44
Toggle description

Review all that we've done in Part 3 of the course and take a look at where we'll be going in Parts 4 and 5.

Part 4: JWT Auth and the API

Toggle description

See what we'll be covering in Part 4 of the course, adding JSON Web Tokens for API authentication and completing a phrases API for the app.

Toggle description

Learn about JSON Web Tokens and how they are used, and then add a login endpoint from which API users can obtain a JWT.

Toggle description

Create a GET endpoint in the API from which API users can request the EmojiPhrases stored in the app repository.

Toggle description

Create a POST endpoint in the API to which API users can send new EmojiPhrases, and test adding data in the Postman REST client.

Conclusion 0:52
Toggle description

Review all that we've done in Part 4 of the course and take a look at where we'll be going in Part 5.

Part 5: Deployment

Toggle description

See what we'll be covering in Part 5 of the course, deploying our Ktor app to Heroku, creating a Docker image, and running the Docker image in a container.

Toggle description

See how to deploy your Ktor app to Heroku, connect Heroku to GitHub, and setup a PostgreSQL database on Heroku for your app.

Toggle description

Add some build flags to support Dockerizing your Ktor app, then write a Dockerfile and use it to build a Docker image of the app.

Toggle description

Run your Ktor app Docker image in a container, including connecting the app container to a Docker container running PostgreSQL.

Conclusion 2:43
Toggle description

Review all that you've done to build a deployable Ktor web app and API, both backed by a PostgreSQL database, and take a look at where to go from here.