Deploying Crystal app to production using Heroku

This article assumes that you have Crystal and Heroku CLI installed.

Create a sample application

Create a new application using
$ crystal init app sample-app
You should see the following on your terminal
$ crystal init app sample-app
      create  sample-app/.gitignore
      create  sample-app/.editorconfig
      create  sample-app/LICENSE
      create  sample-app/README.md
      create  sample-app/.travis.yml
      create  sample-app/shard.yml
      create  sample-app/src/sample-app.cr
      create  sample-app/src/sample-app/version.cr
      create  sample-app/spec/spec_helper.cr
      create  sample-app/spec/sample-app_spec.cr
Initialized empty Git repository in /Users/yourname/Desktop/sample-app/.git/
now cd to the root of the app, $ cd sample-app  and add Kemal to shrad.yml file
name: sample-app
version: 0.1.0
authors:
  - John Doe <[email protected]>
dependencies:
  kemal:
    github: kemalcr/kemal
    branch: master
targets:
  sample-app:
    main: src/sample-app.cr
crystal: 0.23.1
license: MIT
and run $ crystal deps for installing dependencies.

Add a “/” route

Open sample-app.cr file and add replace the contents with the following code
require "kemal"
get "/" do
"Hello World"
end
Kemal.run
now run $ crystal src/kemal_sample.cr  and go to http://localhost:3000on your browser. You should see ur app running and the message ‘Hello World’.

Deploy to Heroku

So far so good 🙂 Now let us deploy our app to Heroku. Run $ heroku create your-custom-name –buildpack https://github.com/crystal-lang/heroku-buildpack-crystal.git You should see something like the following on your terminal.
$ heroku create your-custom-name --buildpack https://github.com/crystal-lang/heroku-buildpack-crystal.git
Creating ⬢ your-custom-name... done
Setting buildpack to https://github.com/crystal-lang/heroku-buildpack-crystal.git... done
https://your-custom-name.herokuapp.com/ | https://git.heroku.com/your-custom-name.git
You are all set. Commit and push to heroku
$ git status
$ git add -A
$ git commit -m "commit message"
$ git push heroku master
To see your app running go to https://your-custom-name.herokuapp.com/ . Congratulations you just deployed your crystal app to production Happy Coding]]>