Skip to content

Instantly share code, notes, and snippets.

@MiyamotoAkira
Created August 9, 2016 19:19
Show Gist options
  • Save MiyamotoAkira/042dae266584936d8c56a8933158de6a to your computer and use it in GitHub Desktop.
Save MiyamotoAkira/042dae266584936d8c56a8933158de6a to your computer and use it in GitHub Desktop.
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