Ktor webapp on Heroku
Here is an example of Ktor webapp implementation with several third party components that can be quickly deployed on Heroku. (you will need just to pay for having the desired scaling for your app if its getting serious).
The Stack
- Ktor Framework
- ReactJS
- Redis Session Manager
- MongoDB integration (Atlas)
- GraphQL integration
- Elasticsearch integration
- Heroku deployment
- testcontainers unit testing
Project
Here is the webapp example.
Client Side
sprint-review-client
: ReactJS client.
This module contained all the client side code and is built by Gradle which is executing yarn webpack build and then copy everything from thedist
folder to the static resources of the module sprint-review-server
for Tomcvat deployment.
Server Side
sprint-review-server
: Ktor server.
This module contains all Kotlin codes server side and provides different ways of packinging it:
- Jar build
- War deployment in a Tomcat with
webapp-runner
(containing the client side webapp in ReactJS like mentioned above).
Inside this module, you will be able:
- to execute the server directly (com.sprintreview.ServerKt#main) in a
Jetty
(with you IDEA for example) - to execute the War deployment for having the same code executed this time in Tomcat:
For instance: java -jar sprint-review-server/build/server/webapp-runner-8.5.30.0.jar --session-store redis sprint-review-server/build/libs/sprint-review-server-0.1-SNAPSHOT.war
(declare a PORT global variable: export PORT=8080
)
Testing
Here are the tests: some unit tests for having a testable server:
- On the endpoints side (with the Ktor test embedded server)
- On the storage side with
testcontainers
:
The testcontainers library is a tool for testing your components with docker instances. For example, MongoDB is tested with a real version of MongoDB and not an embedded version which could lead to many bugs.
(the docker container used for testing Elasticsearch has been deployed with a Dockerfile for having CORS configured.)
Ktor
Ktor framework is a server written in Kotlin that is really simple to use and very powerful. The documentation is enough for doing anything you need.
Here is the main class for deploying the Ktor server.
You will find inside some examples on:
- Routing on endpoints and static content
- CORS configuration
- Basic Auth
- Logging/Tracing
Heroku
The simple Procfile
here is only using the Gradle execution for having all the modules integrated in a War and executed by a Tomcat on your Heroku App (with the Redis session manager configured)
Check the readme of the project for deploying this app in Heroku (simply click on the button)- it will:
- Add the Gradle runner plugin in your heroku project
- Add the Redis Heroku addon for using the HA session manager
- Add the Elasticsearch Heroku addon for using it in your app
For MongoDB, using Atlas which is pretty nice, so there is no addon added here.
- You will have to provide an Atlas Mongo URI
- You will have to provide a Redis URI
This heroku configuration is only for dev purpose: it doesn’t include any scaling conf or private spaces for safety (cause it’s not free! :))
Redis Session Manager
The Heroku Redis :: Redis
addon has been added to manage session ids when having tomcat cluster in execution: here is the Link.
The library is using this context.xml to configure Tomcat.
Tomcat
Here is the web.xml
And here is the Ktor configuration for executing the related function to startup the server.
GraphQL
KGraphQL is a Kotlin library to use easily GraphQL in a Kotlin environment. There is no Apolo usage on reactJS side (no gain so far). You can see in the project simple examples of usage (bound to an endpoint “query” and tested in the unit tests).
MongoDB
KMongo is a library for Kotlin using data classes and the deployment has been done on Heroku connected to Mongo Atlas.You can see in the project simple examples of usage (tested in the unit tests).
Elasticsearch
Elasticsearch low REST client has been integrated but not deployed on Heroku, only for testing purpose. Bonsai is one of the addon on Heroku that can be used quickly.
Finally
If you have remarks, suggestions or questions for everything in there, don’t hesitate to ask or help me! (I’m not Gradle saavy, so if you see anything ugly about my conf, feel free to tell me I’ll be grateful).