Deploying Julia Genie on Heroku
Tags: julialang, genie, heroku
Published: 2023-03-23 20:53:12

The tutorials for Genie, a web framework in the Julia language, are pretty good, but there were some holes that I wanted to fill in.  In particular, I want to show as directly as possible how to get a Genie app running on Heroku.  I'll assume some knowledge of Heroku.  There's an official tutorial here:

https://genieframework.com/docs/genie/v5.11/tutorials/Deploying-With-Heroku-Buildpacks.html

My recommendation is that you follow most of it, but without using the sample app that's mentioned, as it doesn't work as is.  Instead, use Genie to create a template as shown in these tutorials:

https://genieframework.com/docs/genie/v5.11/tutorials/Developing-Web-Services.html

https://genieframework.com/docs/genie/v5.11/tutorials/Developing-MVC-Web-Apps.html

For example:
 
```
julia> using Genie
julia> Genie.Generator.newapp_fullstack("GenieDemo5")
```

You'll be prompted for a choice of db, and you should use postgres if you're going to deploy to Heroku, so I chose

`3. PostgreSQL`

This creates a Genie app in a new directory GenieDemo5, and also starts the web server.  If you're already running postgres, this should work, but since I'm not, the server crashed.  I normally run postgres via docker, so you could, for example, add a `docker-compose.yml` file to the app that looks like this:

```
version: '3'
services:
  database:
    image: postgres
    ports:
      - 5434:5432
    env_file:
      - .env.dev
    volumes:
      - db_data:/var/lib/postgresql/data
volumes:
  db_data:
```

and a `.env.dev` file that looks like this:

```
POSTGRES_USER=postgres
POSTGRES_PASSWORD=long-password-for-genie-demo5
POSTGRES_DB=geniedemo5
```

The details here aren't important.  For example, I wanted to map the postgres port from 5432 in the container to 5434 on my machine to avoid conflicts.  So if you have something like the above,

`docker-compose up -d`
 
will start your database in the background.  Then you have to set the parameters in `db/connection.yml` to match your configuration, so for example:

```
env: ENV["GENIE_ENV"]

dev:
  adapter:  PostgreSQL
  host:     127.0.0.1
  port:     5434
  database: geniedemo5
  username: postgres
  password: long-password-for-genie-demo5
```

If you've handled the db set up, then your app should start locally. 
 The tutorials don't tell you how to start the server other than creating a new app, but you can do it as follows:

`bin/server`

Browsing to

http://127.0.0.1:8000

will show the Genie welcome page.  Now, we can deploy just this much to Heroku.  Following the tutorial, create an app with the suggested Julia buildpack, for example:

`heroku create geniedemo5 --buildpack https://github.com/Optomatica/heroku-buildpack-julia.git`

This creates the Heroku app, and outputs the app url and the git url.  If you've already created a git repo, it makes the remote for you as well, but if not, you can use the given url like this:

```
git init
git remote add heroku https://git.heroku.com/geniedemo5.git
```

You also need a `Procfile` that looks like this:

```
web: bin/server
```

If you commit, push to Heroku, and watch the logs, you'll see the app build and start, but then crash.  Because we're using an app with different environments set up, you need to do

`heroku config:set GENIE_ENV=prod`

That's enough to get the welcome page working.  Of course, you'd still need to set up the db on Heroku.  Also note that I got memory warnings until I started using a 2x dyno.  I'm not currently running this app since I didn't want to pay for a demo, but the code as described here is on github:

https://github.com/jdwright/GenieDemo5