Giter Club home page Giter Club logo

kotlin-ktor-exposed-starter's Introduction

Kotlin Ktor Build codecov

Starter project to create a simple RESTful web service in Kotlin

Updated for Kotlin 2.0.0 and Ktor 2.3.12

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

Getting Started

  1. Clone the repo.
  2. In the root directory execute ./gradlew run
  3. By default, the server will start on port 8080. See below Routes section for more information.

Libraries used:

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 /widgets --> get all widgets in the database

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

POST /widgets --> 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": 3,
    "name": "new widget",
    "quantity": 64,
    "dateUpdated": 1519926898
}

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

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

Notifications (WebSocket)

All updates (creates, updates and deletes) to Widget instances are served as notifications through a WebSocket endpoint:

WS /updates --> returns Notification instances containing the change type, id and entity (if applicable) e.g:

{ 
    "type": "CREATE", 
    "id": 12, 
    "entity": { 
      "id": 12, 
      "name": "widget1", 
      "quantity": 5, 
      "dateUpdated": 1533583858169 
    }
}

The websocket listener will also log out any text messages send by the client. Refer to this blog post for some useful tools to test the websocket behaviour.

Testing

The sample Widget service and corresponding endpoints are also tested with 100% coverage. Upon startup of the main JUnit suite (via the test source folder), the server is started ready for testing and is torn down after all tests are run.

  • Unit testing of services with AssertJ - DAO and business logic is tested by initialising an in-memory H2 database with Exposed, using the same schema as the main app. With this approach database queries are fully tested without any mocking.
  • Integration testing of endpoints using a fully running server with Rest Assured - routing tests/status codes/response structure. This utilises the fact that Ktor is a small microframework that can be easily spun up and down as part of the test suite. You could also use the special test engine that Ktor provides, however my preference is to always start a full version of the server so that HTTP behaviour can be tested without relying on special internal mechanisms.
  • Code coverage and reporting performed automatically by Kover as part of the Gradle build

kotlin-ktor-exposed-starter's People

Contributors

akurdyukov avatar brunapereira avatar dependabot[bot] avatar frikit avatar nskvortsov avatar raharrison avatar sanao1006 avatar v3lop5 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

kotlin-ktor-exposed-starter's Issues

Can not start app with ./gradlew run

Hey @raharrison ,

this is probably a question only a total Kotlin newbie can ask- I do it anyway :)
I'm looking at different ktor example projects to learn its structure, and while I can start most of them with ./gradlew run it seems I can't start yours. Can you let me know what I'm doing wrong?
I tried to find some explanation in the README. So once I get it to run I'm happy to provide a PR against your README (in case I didn't just miss the explanation).

My assumption is that I can run the service somehow- is run the correct command?

Here's what's going wrong:

tuhlmann:~/.../kotlin/kotlin-ktor-exposed-starter$ ./gradlew run
> Task :run FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':run'.
> No main class specified and classpath is not an executable jar.

Thanks,
Torsten.

bug: application.conf is ignored

the port configuration is hard-coded, and the application.conf is ignored.
The following code would fix the issue

 embeddedServer(Netty, commandLineEnvironment(args)).start(wait = true)

application.conf:    
ktor {
    deployment {
        port = 8000
        watch = [  build ] 
    }

    application {
        modules = [ MainKt.mainModule ]
    }
}

Why is Hikari set to `isAutoCommit = false`

Also, shouldn't the newSuspendedTransaction {} block in DatabaseFactory be using Dispatchers.IO? That's how JetBrains does it in the Ktor docs.

Mostly, I'm just wondering why you would want Hikari set to isAutoCommit = false.

Thanks!

netty -> CIO

Recent Kotlin versions have Coroutine IO (CIO) library. Do you think it's reasonable to migrate netty to CIO for this starter?

unresolved reference:exposed.....

import org.jetbrains.exposed.sql.Table
unresolved reference:exposed.....
i have compile exposed successful like this: compile "org.jetbrains.exposed:exposed:0.10.4"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.