multiple improvements based on Ken Wesson's comments
[propagator.git] / src / web.clj
blob97a9e818a36ed4c7cff6e64f4b037582c5addbaf
2 (load-file "/home/eschulte/src/propagator/src/propagator.clj")
3 (in-ns 'propagator)
4 (use 'ring.adapter.jetty)
5 (use 'clojure.contrib.seq-utils)
6 (import 'java.util.Date 'java.text.SimpleDateFormat)
8 ;; cells
9 (cell names '())
10 (cell input "")
12 (propagator adder [input names] [names]
13             (when (> (count (seq input)) 0)
14               (set (cons (str input " :1") names))))
16 (defn app [req]
17   (or
18    ;; page to accept input
19    (when (= "/" (:uri req))
20      {:status 200
21       :headers {"Content-Type" "text/html"
22                 "Title" "3000"}
23       :body (apply str
24                    "<form name=\"\" action=\"add\" method=\"get\">"
25                    "Word: <input type=\"type\" name=\"word\" />"
26                    "<input type=\"submit\" value=\"Submit\" />"
27                    "</form>")})
28    ;; dump value into "input" cell
29    (when (re-matches #"/add" (:uri req))
30      (set-cell input (second (re-matches #".+=(.+)" (:query-string req))))
31      {:status 303 :headers {"Location" "../list"}})
32    ;; render the value of the "list" cell
33    (when-let [matched (re-matches #"/list" (:uri req))]
34      {:status  200
35       :headers {"Content-Type" "text/html"}
36       :body    (apply str (flatten (list "<ul>"
37                                          (map #(str "<li>"%"</li>") @names)
38                                          "</ul>"
39                                          "<p><a href=\"/\">another word</a></p>")))})))
41 (run-jetty #'app {:port 3000})