Create a gist now

Instantly share code, notes, and snippets.

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