Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
What ?!! Okay, let me clarify. Letâs build a complete backend /web-app with Kotlin and your favourite text-editor only. No, no need to install IntellJ IDEA, No need to handle Gradle file stuff .. seriously, No! All you need is a simple text-editor and Kotlin.So⊠how to do that ?
For this recipe, we need two ingredients (apart from text-editor and Kotlin).I assume that youâve installed Kotlin and you are set with Atom or Sublime or Vim.
Installing Kscript and KTor
A made GIF ;-)What is KTor ?Ktor is a framework for building asynchronous servers and clients in connected systems using the powerful Kotlin programming language.
KTor is a web-framework built with Kotlin to make powerful webapps. This is the framework we are going to use. Ktor is a framework to easily build connected applicationsâââweb applications, HTTP services, mobile and browser applications. Modern connected applications need to be asynchronous to provide the best experience to users, and Kotlin coroutines provide awesome facilities to do it in an easy and straightforward way.If you want to read a more about Ktor check the description here.
What is Kscript ?Enhanced scripting support for Kotlin on *nix-based systems.
This is the interesting part. It is a library maintained by @ HolgerBrandl which provides enhanced and awesome scripting support for Kotlin. Even you can use this tool for data/text processing and also to run Kotlin.kt files. Watch this talk for more details and what Kscript can do.
To install Kscript follow this procedure:If you have the sdkman installed you can skip directly to step 2.1. Install sdkmanYou can follow instructions here. or Run:
$ curl -s âhttps://get.sdkman.io" | bash
Then follow the instructions on the terminal screen to run something like this
$ source "$HOME/.sdkman/bin/sdkman-init.sh"
2. Install KscriptOnce you have installed sdkman, you can run this to install Kscript. (But prerequisite to Kscript is Kotlin and Maven, so make sure youâve Kotlin on your machine. Simply, run $ sdk install kotlin and $ sdk install maven ).Now run the following command:
$ sdk install kscript
Itâll download the Kscript and will add it to your path.
Let me give you a quick overview of Kscript, so that youâll get an idea about it. To get started with Kscript and for complete reference please visit this repo.The extension of file is .kts or you can use .kt . Add this line at top of your file #!/usr/bin/env kscript (point to it in the shebang line). Inside of the file you can write any kotlin code. Check below simple hello_world.kts .
#!/usr/bin/env kscript
import java.util.*
println("Belllloooo!!")//Rest of the code below
And you can Run this using the following command. It has some compilation overhead so itâll take some time.
$ kscript hello_world.kts
Please go through the kscript repo to get more details of how to include library dependency, another file, Kotlin-opts, JVM-args etc.
Running a KTor Server
Now letâs look at KTor. To use KTor, weâve this dependency compile "io.ktor:ktor-server-netty:0.9.4" to use it with gradle. But, as I said, we are not maintaining any gradle file stuff at all. Here comes one of the best features of Kscript, is that you can declare library dependencies in the same file that you are working on. Let me show you what I mean by modifying the same hello_world.kts file.
Here the @file:MavenRepository(...) is same as specifying the repositor{} in build.gradle file in your IntellJ IDEA. The repositories required for KTor are maven repos. Also the the @file:DependsOnMaven(...) specifies the artifact required for basic usage with Netty.
The dependencies will be downloaded and cached when you will try to run the above file for the first time. To clear the cache you can run kscript --clear-cache .
By modifying further the above file letâs see how we can start the server. I am modifying the same hello_world.kts file again.
And youâre done. Just run the command $ kscript hello_world.kts . Itâll download the dependencies for the first time (takes a some time for compilation and downloading dependencies). You can ignore the warnings(something like SL4J etc). Visit the url http://localhost:8080 on your browser. Hope youâll possibly see something there :p
Weâve just created a simple server with a text-editor and a kotlin. For further completion you can check the server-backed development of KTor in the documentation. For client side HTML-templating and requests handling check this documentation of KTor.
KTor is asynchronous server framework. Iâll be covering those topics in the next part of this series. So, stay tuned for the next post.
Part-II is coming. Stay tuned ! Clap for me if you liked the story and follow me to get awesome recipes like this. Visit http://nikhya.me for more stuff.
A complete Backend only with Kotlin and your favourite text-editor! was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.