Question

I have a picture gallery application I made from the "Web Development with Clojure" book and I am at the point of deploying it to Heroku. I have tried making it work as both a standalone uberjar and with trampoline. I tried to use environ in the beginning, but kept getting database value errors on "db-spec" so I stopped using it to make it run fine locally. I tried to set my own environment variables, and also to use a main.clj file. I edited my profile settings info and can get it to deploy but not run properly on heroku. Sometimes I get a blank screen and can navigate to a few of my pages and then sometimes I get an h10 app crashed error on a GET that is looking for a favicon, which is hard to troubleshoot.

Could someone with experience tell me specifically what I need to write and in what files to make it work in production on Heroku? Bonus points if you can also demystify the environment variables and db connections settings with Postgres in Heroku for me.

I have scoured the heroku, environ and leiningen docs. I have also searched for the same problem on stack and of course triple worked through all the examples in my book, which are mirrored by the luminus docs, cause the same guy wrote them. The link to my app is below. Below is a link to one version of the project I put up on github.

https://github.com/gamma235/picture-gallery

This is how I have defined my database:

(def db 
  {:subprotocol "postgresql"
   :subname "//localhost/gallery"
   :user "admin"
   :password "admin"})

This is my main.clj file:

(ns picture-gallery.main
  (:use picture-gallery.handler
        [org.httpkit.server :only [run-server]]
        [ring.middleware file-info file])
  (:gen-class))

(defn -main [& [port]]
  (let [port (if port (Integer/parseInt port) 3000)]
    (run-server app {:port port})
    (println (str "You can view the site at http://localhost:" port)))))

I am using [org.clojure/java.jdbc "0.2.3"] for my database needs. It is the old version but I am following along with the book.

Here are significant parts of my project.clj file:

...    
:main picture-gallery.main
      :min-lein-version "2.0.0"
      :plugins [[lein-ring "0.8.7"]]
      :ring {:handler picture-gallery.handler/app
             :init picture-gallery.handler/init
             :destroy picture-gallery.handler/destroy}
      :profiles
      {:uberjar {:main picture-gallery.main, :aot :all}}
      )

I followed the heroku shouter app tutorial here and deployed it with little fuss. I am unable to figure out how alter the code in my picture-gallery app, based on this project, however. Things like $JVM_OPTS in the Procfile are mysterious to me. Any explanations or referrals are welcome. Please browse the source-code for the heroku app and succeed where I have failed.

Was it helpful?

Solution

I refactored the code to wrap my db with Korma:

(ns picture-gallery.models.db
  (:require [clojure.java.jdbc :as sql]
            [korma.db :refer [defdb transaction]]
            [korma.core :refer :all]))

(def db (or (System/getenv "DATABASE_URL")
             "postgresql://localhost:5432/gallery"))

(defdb korma-db db)

redeployed it and it worked fine. I still have no idea why it wasn't working before and it only started working after I used Korma, changing the db definition to the DATABASE_URL alone didn't do it. I think that it either had something to do with Heroku only supporting the latest version of JDBC (I was using 0.2.3) or connection pooling. As I have no idea why this happened, I feel that this question is still open, but for others who come after me and run into the same problem, use Korma. As a final note, I also changed the templating from Hiccup to Selmer, so there is a chance that this was the cause of the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top