Connecting a Ktor web server to a Corda node
August 12, 2019
The preparation for this blog post began several weeks ago (probably over a month by now). Before I could write about melding Corda and Ktor together, I first needed to lay the groundwork and focus solely on Ktor. That is where my blog post, Ktor — a Kotlin web framework came into existence. If you haven’t used or seen Ktor before, I recommend taking a browse at that post either before or after reading this post. Reading it in advance is probably a better idea, but you are in control of your own life ?.
This post will focus on implementing a Ktor web server that connects to a Corda node. I am not going to talk about why you should use Ktor. That decision is up to you. What I am doing is providing you with some information and allowing you to formulate a decision by yourself (like anything you read on the internet ?).
Dependencies
https://medium.com/media/e681ad195c0bdc23ac48253daff911b3/href
There are a few things to highlight here. First, the kotlin_version_for_app property. Ktor requires Kotlin 1.3 (as it uses coroutines) but, at the time of writing, Corda only supports Kotlin 1.2. Therefore, different versions need to be used for the web server code and the Corda node. Secondly, jackson-module-kotlin is excluded as it causes a runtime error due to a version mismatch.
High level look at the code
Below is a small snippet of code showing the starting point of the web server’s implementation:
https://medium.com/media/97756937e7469c5749cf6620bc69d26b/href
This code ties everything together as all the functionality of the server branches out from the functions above.
The contents of module will be explored in the following sections.
Connecting to the node
I bet you might have a good idea what connectToNode does. I hope you do anyway… Below are the contents of connectToNode:
https://medium.com/media/da81a3d661fae80ff8f7bc30753dfd11/href
If you have seen any of the Corda samples, then you will probably be familiar with this piece of code. Long story short, it connects to the node with the given connection details. I chose to generate the function’s default parameters from the application’s system properties. This implementation is not particularly important, it just connects to the node and could be written in several different ways.
A CordaRPCConnection is returned from the function. Initially, I wanted to return a CordaRPCOps as the connection itself doesn’t do too much. But, without returning the connection, there is no way to gracefully disconnect from a node. In other words, there needs to be a way to call notifyServerAndClose when the server stops. This is explored further down in the post.
Setting up Jackson
Some extra setup needs to be done to properly use Jackson with Corda:
https://medium.com/media/0449b63e8b22d47b7bd828858a14d088/href
The Corda ObjectMapper is initialised with createDefaultMapper, allowing classes like Party or X509Certificate to be serialised or deserialised. This can be important depending on what is being returned from your own API.
The rest of the code is stolen from the ktor-jackson module. It alters the JSON output slightly to be more somewhat more desirable.
Creating the endpoints
HTTP requests are routed to these endpoints:
https://medium.com/media/3e01b55a14a010c5d562a8440e4b64b8/href
Logic wise, there is not much going on here. For a focused explanation of this code, I recommend reading Ktor — a Kotlin web framework as I mentioned earlier.
Gracefully disconnecting from a node
To gracefully disconnect from a node, the web server needs to call CordaRPCConnection.notifyServerAndClose. Implementing this required a bit of work that I wasn’t expecting. Below is the code that triggers notifyServerAndClose:
https://medium.com/media/3cbc32bb6e99e8ba231c19785335166d/href
A shutdown hook is added to the server. As explained in Graceful shutdown of Ktor applications, subscribing to the ApplicationStopped event is not enough to execute code when terminating the application. The shutdown hook calls stop to gracefully close the NettyApplicationEngine that the server runs upon. Leading to the shutdown event being correctly triggered and executed.
That’s all there is
Yes, really, that is all. Implementing a super basic web server does not require much code at all. There isn’t really anything else to write. I have shown you that there is another web framework that can be used to connect to a Corda node. You don’t have to default to Spring just because the Corda samples use them. If you prefer Ktor, use Ktor. If you don’t, don’t. If you did like the look of Ktor, and if you haven’t already, I recommend looking at Ktor — a Kotlin web framework.
A lot of code was excluded from this post as I focused on the more important aspects of the implementation. If you are interested in the rest of the code, you can find it on my GitHub.
If you enjoyed this post or found it helpful (or both) then please feel free to follow me on Twitter at @LankyDanDev and remember to share with anyone else who might find this useful!
Originally published at https://lankydan.dev on August 12, 2019.
Connecting a Ktor web server to a Corda node was originally published in Corda on Medium, where people are continuing the conversation by highlighting and responding to this story.