An Introduction to Kotlin for server-side applications — Part-1
Over the past decades, a significant change occurred in web and mobile application development. Java and the JVM remain an important platform for server-side programming but popular frameworks like Ruby on rails have been joined by other toolsets like Node.js and Go language. On the web, client-side JavaScript frameworks like React and Angular have gained prominence, while mobile clients for iOS and Android are now primarily built using Swift and Kotlin. One of the benefits specific to Node.js is that developers can focus on using a single language, JavaScript, to develop both the front-end and back-end parts of the system. Using a single language helps development teams focus on the application requirements, spend less time mastering multiple languages, and reduces the number of toolsets you need at your disposal.
The emergence of Swift and Kotlin within mobile development provides a similar opportunity for developers to focus on a single language while building both front-end and back-end systems. Also, both languages are highly performant and statically typed, so they also make it possible to develop safer back-end apps that are fast and have fewer bugs than what you might see in dynamically typed languages like JavaScript or Python.
Kotlin has an edge over other SDK as it not only covers front-end development on mobile as well as web but also server-side development. You can create not only cool mobile and web apps using Kotlin but also the web services that they interact with!. On the server-side, there are several frameworks available for building web apps and web services with Kotlin.
In this article, we are going to see how to build a server using http4k. which is purely built in Kotlin and it is a set of libraries providing a functional toolkit to serve and consume HTTP services, focusing on simple, consistent, and testable APIs. Hence, whilst it does provide support for various APIs relevant to serving and consuming HTTP, it does not provide every integration under the sun — merely simple points to allow those integrations to be hooked in.
Based on the awesome “Your Server as a Function” paper from Twitter, http4k apps are modeled by composing 2 types of simple, independent functions.
1. HttpHandler
It is an abstraction to process HTTP requests into responses by mapping the first into the latter.
E.g.
This code shows a fully functional application consisting of a single Kotlin function app which we embedded into a Jetty server, one example of available server implementations we may choose from. Note the type HttpHandler here, which represents one of the two essential concepts in the idea of “Server as a Function”
2. Filter
It is an abstraction to add pre and post-processing like caching, debugging, authentication handling, and more to an HttpHandler. Filters are composable/stackable.
E.g.
This is a simple example to catch all errors before making any operation likewise we can set content-type before making any API call using the filter.
Every application can be composed of HttpHandlers in combination with Filters, both of which are simple type aliases for ordinary Kotlin function types.
Routing
Routing in http4k works with arbitrary levels of nesting, which works flawlessly since routing itself results in a new HttpHandler.
E.g.
Typesafe HTTP with Lenses
Basic Definition
A Lens targets a specific part of a complex object to either GET or SET a value. basically, it’s a function — or more precisely 2 functions!
Extract: (HttpMessage) -> X
Inject: (X, HttpMessage) -> HttpMessage
Think of the object as the whole and the field as the part. The getter takes a whole and returns the part of the object that the lens is focused on. The setter takes a whole, and a value to set the part too, and returns a new whole with the part updated part. Remember that a lens can be used to both get and set a part of a whole object.
Lenses in http4k
Following the basic idea of a lens, http4k lenses are bi-directional entities which can be used to either get or set a particular value from/onto an HTTP message.
The corresponding API to describe lenses comes in the form of a DSL which also lets us define the requirement (optional vs. mandatory) of the HTTP part we are mounting a lens on. Since HTTP messages are a rather complex container, we can have lenses focusing on different areas of the messages: Query, Header, Path, FormField, Body.
Use Lens to Retrieve value from HTTP Request
For the above routing example, we could use the Query Lens builder and then invoke() the Lens on the message to extract the target value:
E.g.
We create a bidirectional lens focusing on the query part of our message to extract a required and non-empty name from it. Now, if a client happens to call the endpoint without providing a name query parameter, the lens automatically returns an error since it was defined as “required” and “nonEmpty”.
Use Lens to Set the value in HTTP Request
The example shows how we create an instance of Request and inject a value via one or many lenses. We can use the Lens::inject function to specify the value we want to set into an arbitrary instance of Request.
Final Code will look like below:
Deployment
Kotlin applications can be deployed into any host that supports Java Web applications, including Amazon Web Services, Google Cloud Platform and more.
Summary
I personally learned to appreciate Kotlin a lot in a couple of weeks. Once you’ve made yourself comfortable with the basic concepts, it becomes easy to develop front-end, server-side applications quickly also it opens doors for mobile developers to get familiar with web development. We can say Kotlin is truly a full-stack solution by using it we can develop multi-platform applications. We have seen how we can develop a simple server-side application using a framework which purely built using Kotlin.
In the next part, we will see JSON handling, REST API example, and one small application.