Clojure compojure app in Heroku

Tried out today the clojure compojure app deployment into heroku.com cloud service. Deployment didn’t went as smoothly as one might have expected:

  1. By default it wanted to use Java6 instead of Java7.
  2. App didn’t start.
  3. It wanted to use older (< 2.0) Leiningen by default.
  4. App was oddly named in the heroku’s end,  and resulted non-descriptive domain url.

These all were easy to fix:

  1. Add a new file into your project’s root:
     echo "java.runtime.version=1.7" > system.properties
  2. Add a new file into your project’s root:
     echo "web: lein ring server-headless $PORT" > Procfile
  3. Add into project.clj file:
    :min-lein-version "2.0.0"
  4. You can simply rename the app through the heroku dashboard  and app’s settings pane, with rename functionality. Note that the heroku git repository name also changes, so you have to manually change it into your local project’s ./git/config-file so you can continue pushing into production.

Clojure Schema validation

Schema is a Clojure library for declarative data description and validation. Basically it will throw exception when data is not what it is supposed to be.

Couldn’t find by googling a way to enable date to be either existing or not known (null). So when I figured it out, I wanted to share solution with everybody. The example requires that the person has a name (String), but birthdate (Date) is not required.

1
2
3
4
5
6
7
8
9
(:require [schema.core :as s])
 
(defn person
  [name born]
  (s/validate s/Str name)
  (s/validate (s/either java.util.Date (s/pred nil?)) born)
  {:name name
   :born born
  })

Oh, and it is also possible to combine different types like Integers and Strings for enum validation:

1
2
3
4
5
6
7
8
9
(:require [schema.core :as s])
 
(defn person
  [name hands]
  (s/validate s/Str name)
  (s/validate (s/enum 1 2 "none") hands)
  {:name name
   :hands hands
  })