Create a POC server-app for a CRUD REST API with Heroku and Spark

Florent Blot
6 min readFeb 14, 2019
3D picture by Andy Ximenes on ArtStation

Start your future with Kotlin

Inspired by Back To The Future

Dr. Emmett Brown is driving the DeLorean. He wants to show his server-app to his friend, Marty McFly. But the travel does not go well, his server-app is gone. Will Marty succeed to quickly build a new one?

The DeLorean is moving in the streets of Hill Valey. Dr. Brown, focused, drives slowly whereas Marty, looking throught the window, seems lost in his thoughts. After a few minutes, while the engine purring is the only sound around, the doctor breaks the silence:

– Would you like to see how I managed to turn this DeLorean into a time-machine? After what we been through, I think you deserve it.
– Really, Doc? Ah… Are you serious? I’d love to!
– Well young man, carries on Emmett showing a delighted smile, put your seat belt on, we’ll go in 2142!

Emmett sets the date on the car’s dashboard and pushes the accelerator. The DeLorean’s engine thrums out loud, and the car begins to shake.

– When this baby hits 88 miles per hour, shouts Dr. Brown, you gonna see some serious shit.
– Doc, wait! Your car doesn’t have seat bel…

A deafening noise breaks out and the DeLorean disappears from the street, surrounding by lightnings.

404 Not Found

They reappear in a storm, the rain lashes the cabin. The monitor of the dashboard pops some weird characters, anything but numeral, constantly changing.

– Marty, I think the DeLorean broke.
– Wait… What are you saying exactly?
– The dashboard pops strange numbers and I cannot read the actual date. Something seems wrong with the time-machine server.
– What should we do, Doc?
– OK, we should not panic, continues the doctor while the rain hits the windscreen even more. I don’t remember, do you have any computer on you?

Marty takes his mini-computer found in 1997 from his pocket and turns it on.

– Hum, continues the doctor whitout paying attention, log you to my account on Heroku, you will see the time-machine server-app. Click on it, and go to…
– Hum, Doc, there is no one “time-machine-server” item, or anything else on your Heroku’s dashboard. Your account has not any app on it!

Emmett Brown takes off his glasses and, confused, looks at his friend.

– Now we can panic, he says.

Dr. Brown parks the car and stops the engine. The dashboard turns off. A thunderclap explodes. “Quick, Marty! Take the notes I wrote, in the glove box, to create a time-machine” says the doctor. The young man takes three of them.

Getting started with Kotlin

On IntelliJ IDEA, create a new Gradle project: File > New > Project... and name it “time-machine-server”:

Add Kotlin dependency in the build.gradle, then it should look like this:

apply plugin: 'kotlin'group 'sample'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8
buildscript {
ext.kotlin_version = '1.3.11'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
repositories {
mavenCentral()
}

dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}

task copyToLib(type: Copy) {
into "$buildDir/libs"
from(configurations.compile)
}

You can change the packages to match this organization:

main
∟ kotlin
∟ app

Finally, create the file Main.kt in app package:

package app

object Main {

@JvmStatic fun main(args: Array<String>) {
println("Hello World")
}
}

PS: these two online notes could be useful to learn more about it:

Getting started with Heroku

Set up Heroku and create a new application named “time-machine-server” from the dashboard’s page:

Then, define a Procfile in the project at the same level than the build.gradle, which should be like this:

web: java $JAVA_OPTS -cp build/classes/*:build/libs/* app.Main

Next, add the Heroku task stage into the build.gradle:

task stage(dependsOn: ['clean', 'build', 'copyToLib'])
build.mustRunAfter clean

Finally, deploy the project on Heroku:

“Deploy” section on Heroku’s app details

PS: The Heroku CLI allows you to see logs about the deploying state and request activities. See more on the online note:

Getting started with Spark

Back to IntelliJ IDEA, edit the build.gradle to add Spark dependencies:

dependencies {
...
compile 'com.sparkjava:spark-kotlin:1.0.0-alpha'
compile 'org.slf4j:slf4j-simple:1.7.12' // logger for Spark
}

Change the Main class to listen to Heroku’s port and to create a simple route with GET:

package appimport spark.Spark.get
import spark.Spark.port
object Main {

@JvmStatic fun main(args: Array<String>) {
setport()
setroute()
}
private fun setport() {
System.getenv("PORT")?.let {
port(it.toInt())
}
}
private fun setroute() {
get("/hello") { _, _ -> "Hello World" }
}
}

Commit your changes and deploy the updated class on Heroku.

PS: more tips can be found on these two online notes:

Marty stops typing on his computer. Going to his UNIX console, he commits his changes and presses the enter key. After a few seconds, the console displays “Build successful” in green.

– Doc, I did it, shouts the yound man in a red jacket, it builds successfully!
– Great Scott, dear friend, you made it.
– How can I see if our server-app is correctly working?
– Nothing simplest, replies the doctor. Go to the route that you previously created with Spark: https://time-machine-server.herokuapp.com/hello.

The boy opens a browser and go to the url:

His eyes sparkling, he holds back a little whoop. Dr. Brown, next to him, pulls out a small note from his inside pocket and holds out to Marty.

Houston, we got a problem!

The old man takes the lead again:

– We can try now. The server-app is not complete, but it should be fine. We will have only one chance… if any.
– The “time-machine-server” on Heroku is not enough to repair the car?
– We will see, kiddo! Otherwise, we will disappear in a space-time continuum’s bug.
– Really comforting, replies the young man with sweat in his back.

Dr. Brown turns the starting key. The engine starts with a loud noise. The monitor on the dashboard initializes without bumping. He presses the buttons of the monitor until it displays October, the 27th 1985. He pushes down the accelerator. Marty clings to his seat. At 88m/h, the lightnings eat completely the car. The DeLorean with his passengers disappears.

Two trails of fire follow the missing car, whearas the license plate, torn off, stays behind.

The DeLorean reappears suddenly in the main street of Hill Valley. It still is night but it is no longer rainning. The engine makes a big noise and the car stops promptly, without any sound next. The date on the dashboard displays Februrary, the 4th 2019…

Sequel: follow Marty and Emmett into their next adventure to create a CRUD REST API server-app using Heroku, Spark, mLab and KMongo, and restart the DeLorean.

--

--

Florent Blot

Maker, Mobile developer, Kotlin & Dart enthusiast, IoT & DIY addict. @Geev: https://www.geev.com