Thursday, April 08, 2010

My Clojure, MongoDB, Ring, and Compojure development setup

I wrote a few days ago about my Clojure and Compojure setup that automatically reloads modified files while I am developing. I have added support for accessing MongoDB using the congomongo library. My new project.clj file:
(defproject kbsportal "0.1.0"
:description "test using Compojure for KBSportal.com"
:dependencies
[[compojure "0.4.0-SNAPSHOT"]
[ring/ring-devel "0.2.0-RC2"]
[ring/ring-httpcore-adapter "0.2.0-RC2"]
[ring/ring-jetty-adapter "0.2.0-RC2"]
[ring/ring-servlet "0.2.0-RC2"]
[org.clojars.liebke/congomongo "1.0.0"]]
:main kbsportal)
My Compojure setup file kbsportal.clj:
(ns kbsportal
(:use compojure.core
ring.adapter.jetty)
(:use ring.middleware.reload)
(:use ring.middleware.stacktrace)
(:use somnium.congomongo)
(:use mongo)
(:use nlp))

;; set up test use of congomongo + MongoDB:
(mongo!
:db "notes", :host "127.0.0.1") ; notes contain title, content, and words

(defroutes test-routes
(GET "/" []
(str "<h1>Hello World" (foo) "</h1>"
(get-all-notes)))
(ANY "*" []
{:status 404, :body "<h1>Page not found</h1>"}))

(def app
(-> (var test-routes)
(wrap-reload '(kbsportal))
(wrap-reload '(nlp))
(wrap-reload '(mongo))
(wrap-stacktrace)))

(defn dev []
(run-jetty #'app {:port 8080}))
The example file nlp.clj has not changed since my previous blog, and here is the source file mongo.clj for reading all notes from an existing MongoDB data store:
(ns mongo
(:use somnium.congomongo))

;; expect notes to contain :title and :content
(defn get-all-notes []
(let [notes (fetch :notes :only [:title :content])]
(apply
str
(for [note notes]
(str "<b>" (:title note) "</b> " (:content note) "<br/>")))))
Please refer to my previous blog for directions for running this.

1 comment:

Carsten said...

How did you add the first notes to your db to test the output? Just connect to the running mongodb and add them on the console interface?