-
-
Save weavejester/663389 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns demo.core | |
(:use [ring.adapter.jetty :only (run-jetty)] | |
[ring.middleware.session :only (wrap-session)] | |
[ring.middleware.flash :only (wrap-flash)] | |
[ring.middleware.stacktrace :only (wrap-stacktrace)] | |
[hiccup.core :only (html)] | |
[hiccup.form-helpers :only (form-to label text-field submit-button)] | |
[hiccup.page-helpers :only (xhtml unordered-list)] | |
[compojure.core :only (defroutes GET POST)] | |
[compojure.route :only (not-found)] | |
[ring.util.response :only (redirect)])) | |
(declare *flash*) | |
(defn wrap-flash-binding [handler] | |
(fn [request] | |
(binding [*flash* (request :flash)] | |
(handler request)))) | |
(defn message [title & message] | |
(html [:h2 title] | |
[:p message])) | |
(defn html-doc | |
[title & body] | |
(xhtml {:lang "en"} | |
[:head [:title title]] | |
[:body | |
[:h1 title] | |
(if *flash* | |
(message "Flash" *flash*)) | |
body])) | |
(defn index-resource [errors count num] | |
(html-doc "Demo" | |
(if count | |
(message "Count!" | |
"The count is now " (or count 0)) | |
[:h2 "A form"] | |
(form-to [:post "/"] | |
(if-let [msg (errors key)] | |
[:p "Error: " msg]) | |
(label :num num) | |
(submit-button "Add")))) | |
(defn parse-int [n] | |
(try (Integer/parseInt n) | |
(catch NumberFormatException _ nil))) | |
(defn assoc-session [response session key value] | |
(assoc-in response [:session key] value)) | |
(defn flash [response & message] | |
(assoc response :flash (apply str message))) | |
(defn invalid-number [key] | |
{key "Please enter a number"}) | |
(defn add-resource [{count :count :as session} num] | |
(if-let [n (parse-int num)] | |
(-> (redirect "/") | |
(assoc-session :count (+ n (or count 0)) | |
(flash "Added " n " to " count))) | |
(index-resource (invalid-number :num) count num))) | |
(defroutes main-routes | |
(GET "/" {{{count :count} :session} {{num "num"} :params}} | |
(index-resource {} count num) | |
(POST "/" {{session :session} {{num "num"} :params}} | |
(add-resource session num) | |
not-found) | |
(def app | |
(-> #'main-routes | |
(wrap-flash-binding) | |
(wrap-flash) | |
(wrap-session) | |
(wrap-stacktrace))) | |
(defn boot [] | |
(run-jetty #'app {:join? false :port 8080})) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defproject one-up "0.1.0-SNAPSHOT" | |
:description "Request Demo" | |
:dependencies [[org.clojure/clojure "1.2.0"] | |
[org.clojure/clojure-contrib "1.2.0"] | |
[ring/ring-core "0.3.3"] | |
[ring/ring-jetty-adapter "0.3.3"] | |
[compojure "0.5.2"] | |
[hiccup "0.3.0"]] | |
:dev-dependencies [[ring/ring-devel "0.3.3"] | |
[swank-clojure "1.2.1"]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment