Skip to content

Instantly share code, notes, and snippets.

@matstani
Created August 17, 2014 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matstani/4846ff717e4997139937 to your computer and use it in GitHub Desktop.
Save matstani/4846ff717e4997139937 to your computer and use it in GitHub Desktop.
ringハンドラ内でDatabaseコンポーネントを利用
(ns example-component-ring.handler
(:require [clojure.string :as str]
[compojure.core :refer :all]
[compojure.handler :as handler]
[compojure.route :as route]
[monger.collection :as mc]
[monger.core :as mg]))
(defn get-entries [{:keys [connection]}]
(let [db (mg/get-db connection "mydb")]
(mc/find db "entries" {})))
(defn get-entry-list [db]
(let [entries (get-entries db)]
(str/join "<br />" (map #(get % "title") entries))))
(defroutes app-routes
(GET "/" [:as {db :db}] (get-entry-list db))
(route/resources "/")
(route/not-found "Not Found"))
(def app
(handler/site app-routes))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment