forked from jalehman/react-tutorial-om
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.clj
44 lines (34 loc) · 1.15 KB
/
core.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
(ns react-tutorial-om.core
(:require [compojure.handler :as handler]
[compojure.route :as route]
[compojure.core :refer [GET POST defroutes]]
[ring.util.response :as resp]
[cheshire.core :as json]
[clojure.java.io :as io]))
(def comments (atom []))
(defn json-response [data & [status]]
{:status (or status 200)
:headers {"Content-Type" "application/json"}
:body (json/generate-string data)})
(defn init
[]
(reset! comments (-> (slurp "comments.json")
(json/parse-string true)
vec)))
(defn save-comment!
[{:keys [body]}]
(let [comment (-> body io/reader slurp (json/parse-string true))]
(swap! comments conj comment)
(json-response
{:message "Saved comment!"})))
(defroutes app-routes
(GET "/" [] (resp/redirect "/index.html"))
(GET "/comments" [] (json-response
{:message "Here's the comments!"
:comments @comments}))
(POST "/comments" req (save-comment! req))
(route/resources "/")
(route/not-found "Page not found"))
(def app
(-> #'app-routes
handler/api))