Heroku

There is a quickstart repository for Heroku: <https://github.com/orangy/ktor-heroku-start>

Preparing

For using Heroku, you will need Java, Maven/Gradle and the Heroku CLI

You will also need to configure your public key in the Heroku configuration.

You can try the heroku —version command to see if you have the command line installed:

1. > heroku --version
2. heroku-cli/6.15.36 (darwin-x64) node-v9.9.0

You will also need an app.json file describing your projects and your dependencies:

1. {
2. "name": "Start on Heroku: Kotlin",
3. "description": "A barebones Kotlin app, which can easily be deployed to Heroku.",
4. "image": "heroku/java",
5. "addons": [ "heroku-postgresql" ]
6. }

You will also need a Procfile describing what to execute:

1. web:    java -jar target/helloworld.jar

And a system.properties file describing your java version:

1. java.runtime.version=1.8

Running locally

And a file called .env along with the other files(required for development).This will contain environment variables that Heroku will pass to the application.For example, for the quickstart:

1. PORT=8080
2. JDBC_DATABASE_URL=jdbc:postgresql://localhost:5432/java_database_name

If your local installation of postgresql has a user/password, you have to change the jdbc url too:

1. JDBC_DATABASE_URL=jdbc:postgresql://localhost:5432/java_database_name?user=user&password=password

You will also first need to create the database:

1. > psql -c "CREATE DATABASE java_database_name;"

3. CREATE DATABASE

With these files, you can use Gradle or Maven to create a fat-jar and adjust the Procfileto point to the right file.

After building the jar, in Unix systems you can use heroku local:start to start your server.

Deploying

You first have to create an app or set the git remote. heroku create will create an appwith a random available name and it will set a git remote of the repo.After calling heroku create, you should see something like this:

1. > heroku create
2. Creating app... done, ⬢ demo-demo-12345
3. https://demo-demo-12345.herokuapp.com/ | https://git.heroku.com/demo-demo-12345.git

This effectively adds a heroku remote to your git clone:

1. > cat .git/config
2. ...
3. [remote "heroku"]
4. url = https://git.heroku.com/demo-demo-12345.git
5. fetch = +refs/heads/*:refs/remotes/heroku/*

After that, you have to push your git changes to the heroku remote. And it does a build on push:

1. > git push heroku master
2. Counting objects: 90, done.
3. Delta compression using up to 4 threads.
4. Compressing objects: 100% (59/59), done.
5. Writing objects: 100% (90/90), 183.08 KiB | 5.55 MiB/s, done.
6. Total 90 (delta 21), reused 0 (delta 0)
7. remote: Compressing source files... done.
8. remote: Building source:
9. remote:
10. remote: -----> Java app detected
11. remote: -----> Installing JDK 1.8... done
12. remote: -----> Executing: ./mvnw -DskipTests clean dependency:list install
13. ...
14. remote:        [INFO] BUILD SUCCESS
15. remote:        [INFO] ------------------------------------------------------------------------
16. remote:        [INFO] Total time: 49.698 s
17. remote:        [INFO] Finished at: 2018-03-23T04:33:01+00:00
18. remote:        [INFO] Final Memory: 41M/399M
19. remote:        [INFO] ------------------------------------------------------------------------
20. remote: -----> Discovering process types
21. remote:        Procfile declares types -> web
22. remote:
23. remote: -----> Compressing...
24. remote:        Done: 60.7M
25. remote: -----> Launching...
26. remote:        Released v4
27. remote:        https://demo-demo-12345.herokuapp.com/ deployed to Heroku
28. remote:
29. remote: Verifying deploy... done.
30. To https://git.heroku.com/demo-demo-12345.git
31. * [new branch]      master -> master

Now you can execute heroku open to open your application in your browser:

1. heroku open

In this case, it will open: <https://demo-demo-12345.herokuapp.com/>

If you encountered:

1. remote:  !     ERROR: Failed to run Gradle!
2. remote:        It looks like your project does not contain a 'stage' task, which Heroku needs in order
3. remote:        to build your app.

It probably means that Heroku doesn’t know how to build your app and you have to set it manually. For example, if you used shadowJar you have to set it using:

1. > heroku config:set GRADLE_TASK="shadowJar"
2. Setting GRADLE_TASK and restarting ⬢ demo-demo-12345... done, v4
3. GRADLE_TASK: shadowJar

Remember that Heroku sets an environment variable called PORT which you have to bind to instead ofa fixed port.When using embeddedServer you will have to use System.getenv, while when using application.conf you willhave to set ktor.deployment.port = ${PORT}.Check out the page aboutusing environment variables in the configurationfor more information.