The Heroku blog has a good description on running Clojure http://blog.heroku.com/archives/2011/7/5/clojure_on_heroku but let’s do it ourselves.
This is very similar to the previous post Running Clojure on the AWS Beanstalk
The source is on http://github.com/ctdean/heroku-hello
We’re assuming that you have lein and Java installed and that you’ve installed the tools from https://devcenter.heroku.com/articles/quickstart
We’ll use http://webnoir.org/ to build the app. First the app itself.
(ns ctdean.heroku.hello
(:use noir.core hiccup.core)
(:require [noir.server :as server]))
;; The only page
(defpage "/" []
"<html><body>Hello world</body></html>")
;; The entry point for Heroku
(defn -main [& m]
(let [mode (keyword (or (first m) :dev))
port (Integer. (get (System/getenv) "PORT" "5000"))]
(server/start port {:mode mode
:ns 'ctdean.heroku.hello})))
The equally important project.clj
(defproject ctdean-heroku-hello "1.0.0"
:dependencies [[org.clojure/clojure "1.3.0"]
[noir "1.2.2"]]
:main ctdean.heroku.hello)
And the Procfile
that Heroku uses.
web: lein run -m ctdean.heroku.hello
Let’s try it out:
% foreman start &
You can look at it in your browser at http://localhost:5000, or on the command line:
% curl http://localhost:5000
<html><body>Hello world</body></html>
Heroku uses git to push out the code so we need to create the git repo:
% git init .
% echo target >.gitignore
% git add README.md Procfile project.clj .gitignore src/ test/
% git commit -m 'Initial checkin'
and then we create the Heroku app:
% heroku create --stack cedar
When the app is created it will assign a default url, in my case it was http://blazing-winter-3109.herokuapp.com
And then push it out:
% git push heroku master
The app should now be running on the assigned url. You can view it in your browser using the
% heroku apps:open
11 Apr 2012