- Notifications
You must be signed in to change notification settings - Fork76
raharrison/kotlin-ktor-exposed-starter
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Updated for Kotlin 2.2.0 and Ktor 3.2.0
Companion article:https://ryanharrison.co.uk/2018/04/14/kotlin-ktor-exposed-starter.html
- Clone the repo.
- In the root directory execute
./gradlew run
- By default, the server will start on port
8080
. See belowRoutes section for more information.
- Ktor - Kotlin async web framework
- Netty - Async web server
- Kotlin Serialization - JSON serialization/deserialization
- Exposed - Kotlin SQL framework
- H2 - Embeddable database
- HikariCP - High performance JDBC connection pooling
- Flyway - Database migrations
- JUnit 5,AssertJandRest Assured for testing
- Kover for code coverage, publishingtoCodecov through GitHub Actions
The starter project creates a new in-memory H2 database with one table forWidget
instances.
As ktor is async and based on coroutines, standard blocking JDBC may cause performance issues when useddirectly on the main thread pool (as threads must be reused for other requests). Therefore, another dedicated threadpool is created for all database queries, alongside connection pooling with HikariCP.
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
All updates (creates, updates and deletes) toWidget
instances are served as notifications through a WebSocket endpoint:
WS /updates
--> returnsNotification
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 tothis blog post for some useful tools to test the websocket behaviour.
The sample Widget service and corresponding endpoints are also tested with 100% coverage. Upon startup of the main JUnit suite (via thetest
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 withExposed, using the same schema as the main app. With this approach database queries are fully tested without anymocking.
- Integration testing of endpoints using a fully running server with Rest Assured - routing tests/status codes/responsestructure. This utilises the fact that Ktor is a small microframework that can be easily spun up and down as part ofthe test suite. You could also use the special test engine thatKtor provides,however my preference is to always start a full version of the server so that HTTP behaviour can be tested withoutrelying on special internal mechanisms.
- Code coverage and reporting performed automatically by Kover as part of the Gradle build
About
Starter RESTful service with websocket notifications using Kotlin, Ktor and Exposed with H2, HikariCP and FlyWay
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.