Parcourir la Source

Serve todo-list and current tasks from the web.

This definitely took to long for a few reasons:
- `wrap-response` needs a `{:dirs ["non-standard-dir"]}` parameter to
  work with weird directories
- to have `wrap-restful-response` pickup the clojure data returned by
  some routes you need to wrap your response in a `response` call
  (this wasn't obvious to me because strings were returned just fine and
  in the examples I didn't see anything like it before. a hint in the
  readme would probably help quite a few people. or just actually
  reading the docs...)
Lucas Stadler 13 ans auparavant
Parent
commit
dd946ccdba
2 fichiers modifiés avec 31 ajouts et 2 suppressions
  1. 5 1
      clj/project.clj
  2. 26 1
      clj/shame.clj

+ 5 - 1
clj/project.clj

@ -1,5 +1,9 @@
1 1
(defproject lp-clj "0.0"
2 2
  :description "Playing around with Clojure"
3 3
  :dependencies [[org.clojure/clojure "1.4.0"]
4
                 [org.clojure/data.json "0.2.1"]]
4
                 [org.clojure/data.json "0.2.1"]
5
                 [compojure "1.1.5"]
6
                 [ring-middleware-format "0.2.4"]]
7
  :plugins [[lein-ring "0.8.2"]]
8
  :ring {:handler shame/serve}
5 9
  :source-paths ["."])

+ 26 - 1
clj/shame.clj

@ -1,5 +1,12 @@
1 1
(ns shame
2
  (:use [clojure.data.json :as json :only []]))
2
  (:use [clojure.data.json :as json :only []])
3
  (:use compojure.core)
4
  (:require [compojure.handler :as handler]
5
            [compojure.route :as route])
6
  (:use ring.util.response)
7
  (:use ring.middleware.reload)
8
  (:use ring.middleware.stacktrace)
9
  (:use [ring.middleware.format-response :only [wrap-restful-response]]))
3 10
4 11
; roadmap:
5 12
;  1. implement add-item, resurrect-item, close-item
@ -43,8 +50,26 @@
43 50
(def ^:dynamic *shaming*
44 51
  (ref nil))
45 52
53
;; "backend"
54
46 55
(defn read-shame [filename]
47 56
  (json/read-str (slurp filename) :key-fn keyword))
48 57
49 58
(defn write-shame [filename shaming]
50 59
  (spit filename (json/write-str shaming)))
60
61
;; web service
62
63
(dosync
64
  (ref-set *shaming* (read-shame "self.todo.json")))
65
66
(defroutes shame-routes
67
  (GET "/" [] (response @*shaming*))
68
  (GET "/current" [] (response (:current @*shaming*)))
69
  (route/not-found "404 - Alternate Reality Monsters"))
70
71
(def serve
72
  (-> (handler/site shame-routes)
73
    (wrap-reload {:dirs ["."]})
74
    (wrap-stacktrace)
75
    (wrap-restful-response)))