Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple Liberator application
(ns libtest2.core
(:require [liberator.core :refer [defresource]]
[liberator.dev :refer [wrap-trace]]
[compojure.core :refer [defroutes ANY]]
[clojure.data.json :as json]
[clojure.java.io :as io]))
(defonce appointments (ref []))
(defresource handle-appointments []
:allowed-methods [:post :get]
:available-media-types ["application/json"]
:post! (fn [ctx] (let [body (get-in ctx [:request :body])
converted (slurp (io/reader body))
data (json/read-str converted :key-fn keyword)]
(dosync (alter appointments conj data))))
:handle-ok (fn [_] (json/write-str @appointments)))
(defroutes app
(ANY "/appointments" [] (handle-appointments)))
(def handler
(-> app
(wrap-trace :header :ui)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment