Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Using Kotlin With Ktor to Create Web Apps

Using Kotlin With Ktor to Create Web Apps

The web application framework ktor, written in Kotlin, is meant to provide a tool for quickly creating web applications with Kotlin. The resulting software may be hosted in common servlet containers, like Tomcat, or standalone in a Netty, for example. Whatever kind of hosting you choose, ktor is making heavy use of Kotlin coroutines, so that it’s implemented 100% asynchronously and mainly non-blocking. Ktor does not dictate which frameworks or tools you use, so you can choose whatever logging, DI, or templating engine you like. The library is pretty light-weight in general but is still very extensible through a plugin mechanism.

Kevin Janvier Chinabalire

February 23, 2018
Tweet

Other Decks in Technology

Transcript

  1. Java private String name = “ClinicPesa"; // private
 String name

    = "ClinicPesa"; // default
 protected String name = "ClinicPesa"; // protected
 public String name = "ClinicPesa"; // public Kotlin private val name: String = "ClinicPesa" // private
 protected val name: String = "ClinicPesa" // protected
 public val name: String = "ClinicPesa" // public open class Outer {
 internal val number: Int = 1 // internal val with immutable property
 protected class Nested { public var name: String? = “ClinicPesa" } // public var with mutable property
 } class Subclass : Outer() {} // Outer.number and Nested.name are visible here class Unrelated(o: Outer) {} // Outer.number can be accessed here because it has same module, but Nested.name is not visible here because is in protected class
 internal - visible everywhere but only at the same module, and module is a set of kotlin files compiled together Java vs Kotlin Modifiers
  2. Java private String Student;
 private int number;
 
 public String

    geStudent() {
 return Student;
 }
 
 public int getNumber() {
 return number;
 } Kotlin val Student: String
 val number: Int
 
 fun getUser(): String = user
 
 fun getNumber(): Int = number Method with Return Type
  3. The web application framework ktor, written in Kotlin, is meant

    to provide a tool for quickly creating web applications with Kotlin. K T O R The resulting software may be hosted in common servlet containers, like Tomcat, or standalone in a Netty, for example. Whatever kind of hosting you choose, ktor is making heavy use of Kotlin coroutines, so that it’s implemented 100% asynchronously and mainly non- blocking.
  4. One of the greatest advantages attributed to Kotlin is its

    ability to provide type-safe builders, also known as Domain Specific Languages (DSL). Many libraries already provide DSLs as an alternative to common APIs, such as the Android library Anko, the Kotlin html builder, or the freshly released Spring 5 framework. As we will see in the upcoming examples, ktor also makes use of such DSLs, which enable the user to define the web app’s endpoints in a very declarative way.
  5. Steps Add Ktor dependencies and configure build settings maven {

    url "http://dl.bintray.com/kotlin/ktor" } maven { url "https://dl.bintray.com/kotlin/ kotlinx" } In the following example, a small RESTful web service will be developed with ktor. The service will use an in- memory repository with simple Person resources, which it exposes through a JSON API. Let’s look at the components.
  6. Coroutines are still an experimental feature in Kotlin, so we

    will need to tell the compiler that we are okay with using them to avoid warnings ext.ktor_version = ‘0.9.1' Now we add ktor-server-core module using ktor_version we specified kotlin{ experimental { coroutines "enable" } } Engine and configure it compile "io.ktor:ktor-server-netty:$ktor_version" compile "ch.qos.logback:logback-classic:1.2.1" logging to remove these warning messages and get a better idea of what is happening with the app
  7. fun main(args: Array<String>) { // println("Kotlin Working now") /** *

    EmbeddedServer create a server for you and it create the listernes */ val server = embeddedServer(Netty, 8081){ routing { get("/"){ call.respondText("Hello Kotlines") val log= LoggerFactory.getLogger("dd") log.info("Server is running now") } } } server.start(true) S E L F H O S T We can read a File .Json fun getjsonData(fileName: String): String { val file = File(fileName) return file.readText(Charset.defaultCharset()) }