Building Web Applications in Kotlin Using Ktor

If you are looking to build scalable web services or clients on the JVM then Ktor is for you. Written in Kotlin, this is an extensible, lightweight framework that uses coroutines to provide an easy to test and easy use asynchronous programming model.
Course info
Level
Intermediate
Updated
May 31, 2019
Duration
2h 12m
Table of contents
Course Overview
Building Your First Ktor Application
Adding a REST API
Adding More Routes
Testing the Application
Adding an HTML Front End
Adding Services and Dependency Injection with Koin
Securing the UI
Using Sessions for State
Securing the REST API
Description
Course info
Level
Intermediate
Updated
May 31, 2019
Duration
2h 12m
Description

Many web applications and services are built using the Spring Framework. While Spring is incredibly powerful and useful nobody can accuse it of being lightweight. Many developers look for something that ‘just works’, that does not have too much overhead. In this course, Building Web Applications in Kotlin Using Ktor, you will learn about the framework that more and more JVM developers are looking at as a programming language because of the benefits it has such as support of immutable data and null detection, as well as having less of the bloat of Java. Bring these two together, a lightweight HTTP framework and a nice programming language and you get Ktor. First, you will explore Ktor - an open source project developed by JetBrains, the creators of Kotlin. It is heavily based on Kotlin coroutines and so supports a high degree of asynchrony. Next, you will discover how you can use Ktor to develop both REST and HTML based web sites that are secure and extensible. Finally, you will gain an understanding of how to produce complex server-based applications or standalone HTTP client applications. When you are finished with this course, you will know how to create web applications and REST APIs in Ktor.

About the author
About the author

Kevin has spent way too many years in the software industry. Starting on PL/1 on IBM mainframes then graduating through dBase IV to Windows and eventually onto Java, .Net and now JavaScript where he finally thinks he has found a home until the next new shiny comes along.

More from the author
Java Web Fundamentals
Intermediate
3h 22m
Jun 5, 2019
Kotlin: Using Coroutines
Advanced
3h 50m
Aug 10, 2018
More courses by Kevin Jones
Section Introduction Transcripts
Section Introduction Transcripts

Course Overview
(Music) Hi everyone. My name is Kevin Jones, and welcome to my course, Building Web Applications in Kotlin Using Ktor. I am a developer and owner at Rock Solid Knowledge, a software development company based in the United Kingdom. Kotlin is a language that has taken the Java and Android worlds by storm. Ktor is an HTTP server and client framework built using Kotlin and written by JetBrains. This course is an introduction to developing Ktor applications. I concentrate mostly on server-side code, but also cover client-side topics. Some of the major topics we will cover include writing REST APIs, building websites using templates, rooting incoming requests based on verbs and HTTP headers, securing the web application and the REST API using OAuth, and using dependency injection, writing tests and mocking. By the end of this course, you'll know how to create securer, scalable, robust web applications and REST APIs in Ktor. Before beginning the course, you should be familiar with programming Kotlin. I hope you will join me on this journey to learn Ktor with the Building Web Applications in Kotlin Using Ktor course at Pluralsight.

Adding an HTML Front End
In the previous module we talked about testing our application. In this module, we'll look at adding an HTML front end. Welcome back to the Building Web Applications in Kotlin Using Ktor class. My name is Kevin Jones. So in this module, we'll first create a new web project, and this web project will simply be an HTML front end. Into this project we'll add another feature, and this is the StatusPages feature. So this feature allows you to specify how to handle certain statuses within the application. So for example, what happens if I get a 404 error? What happens if I get an unknown user? How do I display that information back to the end user? We'll need to add static content so how do I handle images in JavaScript and CSS for example? And then finally, we'll need to add dynamic content and to do that we'll use templates. So before we add all of these features, let's go and create the initial website.

Adding Services and Dependency Injection with Koin
In the previous module, we added an HTML front end to the application. In this module, we'll add more services, and we'll take a look at how we set up the services and do dependency injection inside Ktor. So to do the dependent injection we'll use a library called Koin. So welcome back to the Building Web Applications in Kotlin with Ktor class. My name is Kevin Jones. So in this module, we'll first take a look at application design, so how these applications are going to be structured and how they're going to be layered. We need to cover testing as we set up these services, and we need to cover mocking. So we'll look at how we do mocking within Kotlin and within Ktor. We'll see how we add repositories and add services to our application, and we'll see that to do these things we need to use dependency injection, and to do dependency injection we're going to use Koin. So our application will be structured something like this. We have our web front end. That web front end is calling a REST service, and that REST service is getting its data from a database. Within the REST service itself we'll have two layers; we'll have the API layer that is exposed to the web front end, and we'll have the repository layer, and that repository layer is a thing that reaches out and gets the data. So we need to set up these two layers, the API layer and the repository layer within the REST application itself. So let's see how we do that now.

Securing the REST API
So in the previous modules, we looked at how we secured the HTML front end to the application, and we looked at how we could add sessions to the application. Now that we have those two pieces in place, we can look at how we secure the REST API. Welcome back to the Building Web Applications with Kotlin in Ktor class; my name is Kevin Jones. So in this module, we're going to use something called JWT authentication from inside Ktor. Again, we're simply going to use OAuth, but this time we're going to use it to secure the REST API, and we'll secure it for use within our application and for anybody else that might want to use our REST API. So we're going to protect the API from unauthenticated users. So as we have most of the pieces in place for this, and we've seen most of the technology behind this, let's go and add that code now.