From 45c7b4596b1543db7166277146da02254661e2b1 Mon Sep 17 00:00:00 2001 From: Eric Schulte Date: Mon, 19 Apr 2010 14:36:06 -0600 Subject: [PATCH] adding web-server example --- project.clj | 3 ++- propagator.org | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/project.clj b/project.clj index ea5dd08..425c0a3 100644 --- a/project.clj +++ b/project.clj @@ -2,5 +2,6 @@ :description "Concurrent propagator system" :dependencies [[org.clojure/clojure "1.1.0-alpha-SNAPSHOT"] [org.clojure/clojure-contrib "1.0-SNAPSHOT"] - [vijual "0.1.0-SNAPSHOT"]] + [vijual "0.1.0-SNAPSHOT"] + [ring "0.2.0"]] :dev-dependencies [[leiningen/lein-swank "1.0.0-SNAPSHOT"]]) \ No newline at end of file diff --git a/propagator.org b/propagator.org index 08bafc2..cd18043 100644 --- a/propagator.org +++ b/propagator.org @@ -123,6 +123,47 @@ pull a graph from a the propagators saved in the =prop-graph= variable (/ (+ guess (/ x guess)) 2.0))) #+end_src +** web server +Alright, this will use Clojure's [[http://github.com/mmcgrana/ring][Ring]] web server middle-ware. + +So, I guess the best demo here would be some reading/writing through a +MVC setup. + +The =app= will dispatch the incoming data to input cell by the route +at the end of the url, then there can be a couple of output cells +which will render different views of the related data. + +#+begin_src clojure + (use 'ring.adapter.jetty) + (use 'clojure.contrib.list-utils) + (import 'java.util.Date 'java.text.SimpleDateFormat) + + ;; cells + (cell names '()) + (cell input "") + + ;; propagator -- adds value of input to names + (propagator adder [input names] [names] + (when (count (seq input)) (set (cons input names)))) + + ;; web wrapping + (let [out *out*] + (defn app [req] + (binding [*out* out] (println (:uri req))) + (or + ;; dump value into "input" cell + (when-let [matched (re-matches #"/add/(.+)" (:uri req))] + (set-cell input (second matched)) + {:status 303 :headers {"Location" "../list"}}) + ;; render the value of the "list" cell + (when-let [matched (re-matches #"/list" (:uri req))] + {:status 200 + :headers {"Content-Type" "text/html"} + :body (apply str (flatten (list "")))})))) + + (run-jetty app {:port 3000}) +#+end_src + * notes ** look at mutable data stores - http://clojure.org/agents -- 2.11.4.GIT