Skip to content

kodal/kotlin-ktor-exposed-starter

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Starter project to create a simple RESTful web service in Kotlin

Companion article: https://ryanharrison.co.uk/2018/04/14/kotlin-ktor-exposed-starter.html

Libraries used:

  • Ktor - Kotlin async web framework
  • Netty - Async web server
  • Exposed - Kotlin SQL framework
  • H2 - Embeddable database
  • HikariCP - High performance JDBC connection pooling
  • Jackson - JSON serialization/deserialization

The starter project creates a new in-memory H2 database with one table for Widget instances.

As ktor is async and based on coroutines, standard blocking JDBC may cause performance issues when used directly on the main thread pool (as threads must be reused for other requests). Therefore, another dedicated thread pool is created for all database queries, alongside connection pooling with HikariCP.

Routes:

GET /widget --> get all widgets in the database

GET /widget/{id} --> get one widget instance by id (integer)

POST /widget --> add a new widget to the database by providing a JSON object (converted to a NewWidget instance). e.g -

{
    "name": "new widget",
    "quantity": 64
}

returns

{
    "id": 4,
    "name": "new widget",
    "quantity": 64,
    "dateCreated": 1519926898
}

PUT /widget --> update an existing widgets name or quantity. Pass in the id in the JSON request to determine which record to update

DELETE /widget/{id} --> delete the widget with the specified id

About

Starter RESTful service using Kotlin, Ktor and Exposed with H2 and HikariCP

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Kotlin 100.0%