Skip to content

Instantly share code, notes, and snippets.

@ithayer
Created July 4, 2011 19:20

Revisions

  1. ithayer revised this gist May 7, 2012. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions welcome.clj
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    (ns noir-mongo-heroku.views.welcome
    (:require [noir-mongo-heroku.views.common :as common]
    [noir.content.pages :as pages])
    (:require [noir-mongo-heroku.views.common :as common])
    (use noir.core
    hiccup.core
    hiccup.page-helpers)
  2. ithayer revised this gist Jul 5, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wget-welcome
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,2 @@
    # Update welcome.clj to include some simple mongo code.
    wget https://raw.github.com/gist/1063814/d5997229e3e1c7643b6324af9b9eca351f8696ec/welcome.clj -O src/noir_mongo_heroku/views/welcome.clj
    wget https://raw.github.com/gist/1063814/b376a69fc0e6fbaed613eb35bee98ccc72e6be1b/welcome.clj -O src/noir_mongo_heroku/views/welcome.clj
  3. ithayer revised this gist Jul 5, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion welcome.clj
    Original file line number Diff line number Diff line change
    @@ -33,4 +33,4 @@
    {:$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 " (:value counter))])))
    [:p (str "Welcome to noir-heroku, you're visitor " (or (:value counter) 0))])))
  4. ithayer revised this gist Jul 5, 2011. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions new-project-file
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    :dependencies [
    [org.clojure/clojure "1.2.1"]
    [congomongo "0.1.5-SNAPSHOT"] ;; <-- New.
    [noir "1.1.0-SNAPSHOT"]]
  5. ithayer revised this gist Jul 5, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion heroku-update
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,3 @@
    # Update Heroku
    git commit -a -m "Updates"
    git push heroku
    git push heroku master
  6. ithayer revised this gist Jul 5, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wget-welcome
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,2 @@
    # Update welcome.clj to include some simple mongo code.
    wget https://raw.github.com/gist/1063814/4e2a7b82fd66405776fd897694691b5b4c47fdf2/welcome.clj -O src/noir_mongo_heroku/views/welcome.clj
    wget https://raw.github.com/gist/1063814/d5997229e3e1c7643b6324af9b9eca351f8696ec/welcome.clj -O src/noir_mongo_heroku/views/welcome.clj
  7. ithayer revised this gist Jul 5, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions welcome.clj
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    (ns noir-heroku.views.welcome
    (:require [noir-heroku.views.common :as common]
    (ns noir-mongo-heroku.views.welcome
    (:require [noir-mongo-heroku.views.common :as common]
    [noir.content.pages :as pages])
    (use noir.core
    hiccup.core
  8. ithayer revised this gist Jul 5, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wget-welcome
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,2 @@
    # Update welcome.clj to include some simple mongo code.
    wget https://gist.github.com/1063814.js?file=welcome.clj -O src/noir_mongo_heroku/views/welcome.clj
    wget https://raw.github.com/gist/1063814/4e2a7b82fd66405776fd897694691b5b4c47fdf2/welcome.clj -O src/noir_mongo_heroku/views/welcome.clj
  9. ithayer revised this gist Jul 5, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wget-welcome
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,2 @@
    # Update welcome.clj to include some simple mongo code.
    wget https://gist.github.com/1063814.js?file=welcome.clj -O src/noir_heroku/views/welcome.clj
    wget https://gist.github.com/1063814.js?file=welcome.clj -O src/noir_mongo_heroku/views/welcome.clj
  10. ithayer revised this gist Jul 5, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion create-heroku
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    # 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 noir "1.1.0-SNAPSHOT"
    lein plugin install lein-noir "1.1.0-SNAPSHOT"
    # Create new noir project.
    lein noir new noir-mongo-heroku
    cd noir-mongo-heroku
  11. ithayer revised this gist Jul 5, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion create-heroku
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Commands to initialize a simple heroku noir app. You can cut and paste them together.

    # Install latest version of noir.
    # Install latest version of noir. If an older version is installed, remove it first.
    lein plugin install noir "1.1.0-SNAPSHOT"
    # Create new noir project.
    lein noir new noir-mongo-heroku
  12. ithayer revised this gist Jul 5, 2011. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion create-heroku
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    # Commands to initialize a simple heroku noir app. You can cut and paste them together.

    # Install latest version of noir.
    lein install noir "1.1.0-SNAPSHOT"
    lein plugin install noir "1.1.0-SNAPSHOT"
    # Create new noir project.
    lein noir new noir-mongo-heroku
    cd noir-mongo-heroku
  13. ithayer revised this gist Jul 4, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions welcome.clj
    Original file line number Diff line number Diff line change
    @@ -11,11 +11,11 @@
    "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 a map.
    (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*))
    (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)
  14. ithayer revised this gist Jul 4, 2011. 1 changed file with 12 additions and 12 deletions.
    24 changes: 12 additions & 12 deletions welcome.clj
    Original file line number Diff line number Diff line change
    @@ -9,28 +9,28 @@

    (defn split-mongo-url [url]
    "Parses mongodb url from heroku, eg. mongodb://user:pass@localhost:1234/db"
    (let [matcher (re-matcher #"^.*://(.*?):(.*?)@(.*?):(\d+)/(.*)$" url)]
    (when (.find matcher)
    (zipmap [:match :user :pass :host :port :db] (re-groups matcher)))))
    (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 a map.

    (defn maybe-init []
    "Checks if connection and collection exist, otherwise initialize."
    (when (not (connection? *mongo-config*))
    (let [mongo-url (get (System/getenv) "MONGOHQ_URL")
    config (split-mongo-url mongo-url)]
    (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)))
    (authenticate (:user config) (:pass config))
    (or (collection-exists? :firstcollection)
    (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
    {:_id "counter"}
    {:$inc {:value 1} }
    :return-new true :upsert? true)]
    :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 " (:value counter))])))
  15. ithayer revised this gist Jul 4, 2011. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions heroku-update
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    # Update Heroku
    git commit -a -m "Updates"
    git push heroku
  16. ithayer revised this gist Jul 4, 2011. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions wget-welcome
    Original file line number Diff line number Diff line change
    @@ -1 +1,2 @@
    # Update welcome.clj to include some simple mongo code.
    wget https://gist.github.com/1063814.js?file=welcome.clj -O src/noir_heroku/views/welcome.clj
  17. ithayer revised this gist Jul 4, 2011. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions wget-welcome
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    wget https://gist.github.com/1063814.js?file=welcome.clj -O src/noir_heroku/views/welcome.clj
  18. ithayer revised this gist Jul 4, 2011. 1 changed file with 36 additions and 0 deletions.
    36 changes: 36 additions & 0 deletions welcome.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    (ns noir-heroku.views.welcome
    (:require [noir-heroku.views.common :as common]
    [noir.content.pages :as pages])
    (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)]
    (when (.find matcher)
    (zipmap [:match :user :pass :host :port :db] (re-groups matcher)))))

    (defn maybe-init []
    "Checks if connection and collection exist, otherwise initialize."
    (when (not (connection? *mongo-config*))
    (let [mongo-url (get (System/getenv) "MONGOHQ_URL")
    config (split-mongo-url mongo-url)]
    (println "Initializing mongo @ " mongo-url)
    (mongo! :db (:db config) :host (:host config) :port (Integer. (:port config)))
    (authenticate (:user config) (:pass config))
    (or (collection-exists? :firstcollection)
    (create-collection! :firstcollection)))))

    (defpage "/welcome" []
    (maybe-init)
    (let [counter
    (fetch-and-modify
    :firstcollection
    {:_id "counter"}
    {:$inc {:value 1} }
    :return-new true :upsert? true)]
    (common/layout
    [:p (str "Welcome to noir-heroku, you're visitor " (:value counter))])))
  19. ithayer revised this gist Jul 4, 2011. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions heroku-addon
    Original file line number Diff line number Diff line change
    @@ -1 +1,2 @@
    # Configure Heroku to add a small mongo add on for our app.
    heroku addons:add mongohq:free
  20. ithayer revised this gist Jul 4, 2011. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions heroku-addon
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    heroku addons:add mongohq:free
  21. ithayer revised this gist Jul 4, 2011. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions create-heroku
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    # Install latest version of noir.
    lein install 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
  22. ithayer created this gist Jul 4, 2011.
    5 changes: 5 additions & 0 deletions dependencies
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    # 1. Install lein
    wget https://raw.github.com/technomancy/leiningen/stable/bin/lein
    sh lein
    # 2. Install heroku gem
    gem install heroku