Skip to content

Instantly share code, notes, and snippets.

@ithayer
Created July 4, 2011 19:20
Show Gist options
  • Save ithayer/1063814 to your computer and use it in GitHub Desktop.
Save ithayer/1063814 to your computer and use it in GitHub Desktop.
Clojure on Heroku with Noir and Mongo Demo
# Commands to initialize a simple heroku noir app. You can cut and paste them together.
# Install latest version of noir. If an older version is installed, remove it first.
lein plugin install lein-noir "1.1.0-SNAPSHOT"
# Create new noir project.
lein noir new noir-mongo-heroku
cd noir-mongo-heroku
# Create instructions for heroku.
echo 'web: lein run' > Procfile
# Create git repository.
git init
git add .
git commit -m "Initial commit"
# Initialize heroku app.
heroku create --stack cedar
# Push to heroku.
git push heroku master
# 1. Install lein
wget https://raw.github.com/technomancy/leiningen/stable/bin/lein
sh lein
# 2. Install heroku gem
gem install heroku
# Configure Heroku to add a small mongo add on for our app.
heroku addons:add mongohq:free
# Update Heroku
git commit -a -m "Updates"
git push heroku master
:dependencies [
[org.clojure/clojure "1.2.1"]
[congomongo "0.1.5-SNAPSHOT"] ;; <-- New.
[noir "1.1.0-SNAPSHOT"]]
(ns noir-mongo-heroku.views.welcome
(:require [noir-mongo-heroku.views.common :as common])
(use noir.core
hiccup.core
hiccup.page-helpers)
(:use somnium.congomongo)
(:use [somnium.congomongo.config :only [*mongo-config*]]))
(defn split-mongo-url [url]
"Parses mongodb url from heroku, eg. mongodb://user:pass@localhost:1234/db"
(let [matcher (re-matcher #"^.*://(.*?):(.*?)@(.*?):(\d+)/(.*)$" url)] ;; Setup the regex.
(when (.find matcher) ;; Check if it matches.
(zipmap [:match :user :pass :host :port :db] (re-groups matcher))))) ;; Construct an options map.
(defn maybe-init []
"Checks if connection and collection exist, otherwise initialize."
(when (not (connection? *mongo-config*)) ;; If global connection doesn't exist yet.
(let [mongo-url (get (System/getenv) "MONGOHQ_URL") ;; Heroku puts it here.
config (split-mongo-url mongo-url)] ;; Extract options.
(println "Initializing mongo @ " mongo-url)
(mongo! :db (:db config) :host (:host config) :port (Integer. (:port config))) ;; Setup global mongo.
(authenticate (:user config) (:pass config)) ;; Setup u/p.
(or (collection-exists? :firstcollection) ;; Create collection named 'firstcollection' if it doesn't exist.
(create-collection! :firstcollection)))))
(defpage "/welcome" []
(maybe-init)
(let [counter
(fetch-and-modify
:firstcollection ;; In the collection named 'firstcollection',
{:_id "counter"} ;; find the counter record.
{:$inc {:value 1} } ;; Increment it.
:return-new true :upsert? true)] ;; Insert if not there.
(common/layout
[:p (str "Welcome to noir-heroku, you're visitor " (or (:value counter) 0))])))
# Update welcome.clj to include some simple mongo code.
wget https://raw.github.com/gist/1063814/b376a69fc0e6fbaed613eb35bee98ccc72e6be1b/welcome.clj -O src/noir_mongo_heroku/views/welcome.clj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment