Intro to Ktor: The server-side stack

Intro to Ktor: The server-side stack



My earlier article launched Ktor and a few of its primary options for constructing internet purposes. Now, we’ll increase the instance utility developed in that article by including persistent knowledge and HTMX, which can present extra interactive views. This offers us a setup with a whole lot of energy in a comparatively easy stack.

Please see the earlier article for the instance utility code and setup. We’ll construct on that instance right here.

Add persistence to the Ktor-HTMX utility

Step one towards making our utility extra highly effective is so as to add persistent knowledge. The preferred option to work together with an SQL database in Kotlin is with the Uncovered ORM framework. It offers us a few methods to work together with the database, utilizing both a DAO mapping or a DSL. Kotlin’s native syntax means the general really feel of utilizing the ORM mapping layer has much less overhead than others you may need encountered.

We’ll want so as to add just a few dependencies to our construct.gradle.kt, along with these we have already got:


dependencies {
  // present deps...
    implementation("org.jetbrains.uncovered:exposed-core:0.41.1")
  implementation("org.jetbrains.uncovered:exposed-jdbc:0.41.1") 
  implementation("com.h2database:h2:2.2.224")
}

You’ll discover we’ve included the uncovered core and JDBC libraries, in addition to a driver for the in-memory H2 database. We’ll use H2 as a easy persistence mechanism that may simply be converted to an exterior SQL database like Postgres afterward.

Add companies

To begin with, we’ll create a few easy companies that work together with a important service, which talks to the database. Right here’s our QuoteSchema.kt file to this point, which units up the database schema and offers service capabilities for interacting with it:


// src/important/kotlin/com/instance/plugins/QuoteSchema.kt
bundle com.instance.plugins

import kotlinx.coroutines.*
import org.jetbrains.uncovered.sql.*
import org.jetbrains.uncovered.sql.transactions.transaction

object Quotes : Desk() {
    val id: Column = integer("id").autoIncrement()
    val quote = textual content("quote")
    val creator = textual content("creator")

    override val primaryKey = PrimaryKey(id, identify = "PK_Quotes_ID")
}

knowledge class Quote(val id: Int? = null, val quote: String, val creator: String)

class QuoteService {
    droop enjoyable create(quote: Quote): Int = withContext(Dispatchers.IO) {
      transaction {
        Quotes.insert {
          it[this.quote] = quote.quote
          it[this.author] = quote.creator
        } get Quotes.id
      } ?: throw Exception("Unable to create quote")
    }
    droop enjoyable checklist(): Record = withContext(Dispatchers.IO) {
        transaction {
            Quotes.selectAll().map {
                Quote(
                    id = it[Quotes.id],
                    quote = it[Quotes.quote],
                    creator = it[Quotes.author]
                )
            }
        }
    }
}

There’s rather a lot occurring on this file, so let’s take it step-by-step. The very first thing we do is declare a Quotes object that extends DeskDesk is part of the Uncovered framework and lets us outline a desk within the database. It does a whole lot of work for us primarily based on the 4 variables we outline: id, quote, creator, and main key. The id factor can be auto-generated for an auto-increment main key, whereas the opposite two can have their acceptable column sorts (textual content turns into string, for instance, relying on the database’s dialect and driver). 

Uncovered can also be sensible sufficient to solely generate the desk if it doesn’t exist already.

Subsequent, we declare an information class known as Quote, utilizing the constructor fashion. Discover id is marked as optionally available (since will probably be auto-generated). 

Then, we create a QuoteService class with two suspendable capabilities: create and checklist. These are each interacting with the concurrent help in Kotlin, utilizing the IO dispatcher. These strategies are optimized for IO-bound concurrency, which is acceptable for database entry. 

Inside every service technique, we’ve a database transaction, which does the work of both inserting a brand new Quote or returning a Record of Quotes.

Routes

Now let’s make a Database.kt file that pulls within the QuoteService and exposes endpoints for interacting with it. We’ll want a POST for creating quotes and a GET for itemizing them.


//src/important/kotlin/com/instance/plugins/Database.kt 
bundle com.instance.plugins

import io.ktor.http.*
import io.ktor.server.utility.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import java.sql.*
import kotlinx.coroutines.*
import org.jetbrains.uncovered.sql.*
import org.jetbrains.uncovered.sql.transactions.transaction

enjoyable Utility.configureDatabases() {
    val database = Database.join(
        url = "jdbc:h2:mem:take a look at;DB_CLOSE_DELAY=-1",
        person = "root",
        driver = "org.h2.Driver",
        password = "",
    )
    transaction {
        SchemaUtils.create(Quotes)
    }
    val quoteService = QuoteService() 
    routing {
        publish("/quotes") {
          val parameters = name.receiveParameters()
          val quote = parameters["quote"] ?: ""
          val creator = parameters["author"] ?: ""
 
          val newQuote = Quote(quote = quote, creator = creator) 
 
          val id = quoteService.create(newQuote)
          name.reply(HttpStatusCode.Created, id)
        }
        get("/quotes") {
            val quotes = quoteService.checklist()
            name.reply(HttpStatusCode.OK, quotes)
        }
    }
}

We start by utilizing Database.join from the Uncovered framework to create a database connection utilizing customary H2 parameters. Then, inside a transaction we create the Quotes schema, utilizing our Quotes class we outlined in QuoteSchema.kt.

Subsequent, we create two routes utilizing the syntax we developed in the primary stage of this instance and counting on the create and checklist capabilities and Quote class from QuoteSchema.

Don’t overlook to incorporate the brand new perform in Utility.kt:


// src/important/kotlin/com/instance/Utility.kt 
bundle com.instance

import com.instance.plugins.*
import io.ktor.server.utility.*
import io.ktor.server.response.*
import io.ktor.server.routing.*


enjoyable important(args: Array) {
    io.ktor.server.netty.EngineMain.important(args)
}

enjoyable Utility.module() {

  configureTemplating()
  //configureRouting()
  set up(RequestLoggingPlugin)

  configureDatabases()
}

Discover I’ve commented out the previous configureRouting() name, so it gained’t battle with our new routes.

To do a fast take a look at of those routes, we will use the curl command-line software. This line inserts a row:


$ curl -X POST -H "Content material-Sort: utility/x-www-form-urlencoded" -H "Host: localhost:8080" -d "quote=FooBar.&creator=William+Shakespeare" http://localhost:8080/quotes

And this one outputs the prevailing rows:


$ curl http://localhost:8080/quotes

Utilizing HTMX for interactive views

Now let’s leap proper into making a UI to work together with the companies utilizing HTMX. We wish a web page that lists the prevailing quotes and a kind that we will use to submit a brand new quote. The quote can be dynamically inserted into the checklist on the web page, with out a web page reload.

To attain these objectives, we’ll want a route that pulls the whole lot on the outset after which one other route that accepts the shape POST and returns the markup for the newly inserted quote. We’ll add these to the Database.kt routes for simplicity.

Right here is the /quotes-htmx web page that offers us the preliminary checklist and kind:


get("/quotes-htmx") {
        val quotes = quoteService.checklist()    
        name.respondHtml {
          head {
            script(src = "https://unpkg.com/[email protected]") {} 
          }
        physique {
          h1 { +"Quotes (HTMX)" }
          div {
            id = "quotes-list"
            quotes.forEach { quote ->
              div {
                p { +quote.quote }
                p { +"― ${quote.creator}" }
              }
            }
          }
          kind(technique = FormMethod.publish, motion = "/quotes", encType = FormEncType.applicationXWwwFormUrlEncoded) {
            attributes["hx-post"] = "/quotes"
            attributes["hx-target"] = "#quotes-list"
            attributes["hx-swap"] = "beforeend" 
            div {
              label { +"Quote:" }
              textInput(identify = "quote")
            }
            div {
              label { +"Creator:" }
              textInput(identify = "creator")
            }
            button(sort = ButtonType.submit) { +"Add Quote" }
          }
        }
      }
    }

First, we seize the checklist of quotes from the service. Then we begin outputting the HTML, starting with a head factor that features the HTMX library from a CDN. Subsequent, we open a physique tag and render a title (H1) factor adopted by a div with the id of quotes-list. Discover that id is dealt with as a name from contained in the div block, as an alternative of as an attribute on div

Inside quotes-list, we iterate over the quotes assortment and output a div with every quote and creator. (Within the Specific model of this utility, we used a UL and checklist objects. We might have achieved the identical right here.)

After the checklist comes the shape, which units a number of non-standard attributes (hx-post, hx-target, and hx-swap) on the attributes assortment. These can be set on the output HTML kind factor.

Now all we’d like is a /quotes route to just accept the incoming quotes from POST and reply with an HTML fragment that represents the brand new quote to be inserted into the checklist:


publish("/quotes") {
      val parameters = name.receiveParameters()
      val quote = parameters["quote"] ?: ""
      val creator = parameters["author"] ?: ""
      val newQuote = Quote(quote = quote, creator = creator)
      val id = quoteService.create(newQuote)
      val createdQuote = quoteService.learn(id) 
      name.respondHtml(HttpStatusCode.Created) { 
        physique{
        div {
          p { +createdQuote.quote }
          p { +"― ${createdQuote.creator}" }
        }
    }
  }

That is fairly easy. One wrinkle is that Kotlin’s HTML DSL doesn’t prefer to ship an HTML fragment, so we’ve to wrap our quote markup in a physique tag, which shouldn’t be there. (There’s a easy workaround we’re skipping for simplicity, present in this venture known as respondHtmlFragment). It appears seemingly that producing HTML fragments will ultimately grow to be an ordinary a part of the HTML DSL.

Aside from that, we simply parse the shape and use the service to create a Quote after which use the brand new Quote to generate the response, which HTMX will use to replace the UI dynamically.

Conclusion

We went quick and lean with this instance, to discover the essence of Ktor. Nevertheless, we’ve all the weather of a extremely performant and dynamic stack with out a lot overhead. As a result of Kotlin is constructed on high of the JVM it offers you entry to the whole lot Java does. That, coupled with its highly effective union of object-oriented and purposeful programming, and DSL capabilities, makes Kotlin a compelling server-side language. You should use it for constructing purposes with conventional RESTful JSON endpoints, or with dynamic HTMX-powered UIs, as we’ve seen right here.

See my GitHub repository for the whole supply code for the Ktor-HTMX utility instance.

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 
rooshohttps://www.roosho.com
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Latest Articles

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog.