Developing a Finance Management Website #3
Creating a Ktor project
Hello and welcome back!
As I mentioned in my previous post, I am still not sure if the back-end of my application is gonna be based around Ktor + Ebean or Spring Boot + Spring Data JPA. So, since the purpose of this series is to be a very detailed “log” of the whole development process, I think I am going to do BOTH for now. Later we will see which one is looking cleaner and easier to maintain and drop the other one. Who knows, maybe until that happens I can even count on you guys opinions on which one to keep.
So, the very first thing to do to create a Ktor project is check this site out: https://ktor.io/quickstart/generator.html#. This will create you a perfectly configured sample Ktor project. You can use the plugin on Intellij IDEA for this task too but at the moment of this writing, it was failing on me.
So here is a screenshot of how it looks right now:
Oh SPOILER Alert! I decided to call the project Monies! Kinda catchy. Let’s see if it sticks…
This is what my dependencies look like when I started the project on Intellij IDEA. By the way, it’s a Maven project so, sorry Gradle people. As much as I’d like to use Gradle, I am not very familiar with it and I’ve already scratched my head many times trying to fix Maven’s stubbornness that I’m more comfortable to work with it.
Let me talk a bit about the packages I selected so far to start the project:
- Compression — No specific reason other than compress the payload. It’s good if I am gonna host this code on some hosting service that bills me for traffic.
- CallLogging — Just to help me out while developing. Logs are always welcomed.
- CORS — Cross-Origin Resource Sharing is annoying and this package help me deal with it. Check this out if you wanna learn more about CORS.
- Routing — Well, this is the main thing if you wanna create a REST server. It will handle the URLs your front-end will request and respond to it (in the form of JSON most of the time).
- Jackson — You can’t see it on the screenshot, but I also selected it. It will help me to translate JSON to an object and vice-versa.
Also, I’ve added two more packages to my pom.xml file.
Here you can see I’ve added Ebean and Kotlintest.
Ebean is the ORM I’m gonna use to talk with the database and Kotlintest is what I’m going to use for my Unit tests. I like this tool very much because it’s syntax makes it so much easier to understand the tests. Specially if you are a newbie on TDD, like myself.
Everything looks great. Let’s have a look on my project tree, just so you can see what it looks like so far.
Here I’ve changed a bit already. I’ve created the package com.marcelustrojahn and inside it, three more sub-packages: application, models and services.
This is just to keep things organized.
In application, I will put everything related to the “boot up” process of my project.
In models, everything related to the database.
Services will hold everything that will connect the application to the models. It’s the service layer. If I have to save, read or delete something from my database I will do it by calling a method on the service layer. Theses methods will handle everything that needs to be done BEFORE the deletion so the application won’t break because of a missing relationship or something…
Well, to wrap things up for now let’s just see if the project runs?
Easy, all I have to do is to create a run configuration. So I’m gonna click on that “Add Configuration” button at the top right of Idea’s main window and this screen will show up:
From there I click on the top left button with the PLUS sign and select “Application” then I fill the “Name” textbox with any name I want (I just named it Application too) and then I select the main class that will be the entry point of the application.
Now all we have to do is click on the PLAY button and see if it works:
Oops!? I forgot, since I changed the packages around I should also fix Ktor’s application.conf file to reflect my changes..
Let’s try again:
There!! It’s alive! See you on the next post.
Originally published at https://www.marcelustrojahn.com on August 31, 2019.