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

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 sdkman You can follow instructions here. or Run:

Then follow the instructions on the terminal screen to run something like this

2. Install Kscript Once 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:

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 .

And you can Run this using the following command. It has some compilation overhead so it’ll take some time.

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.ktsfile.

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 !