-
-
Save rickmode/664675 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 link-to 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* (:flash request)] | |
(handler request)))) | |
(defmacro with-flash | |
"Use this to add a flash message to response being built, | |
as when there is an error processing a POST." | |
[flash & body] | |
`(binding [*flash* ~flash] ~@body)) | |
(defn message [title & message] | |
(html [:h2 title] | |
[:p message])) | |
(defn html-doc | |
[title & body] | |
(xhtml {:lang "en"} | |
[:head [:title title]] | |
[:body | |
[:h1 (link-to "/" title)] | |
(if *flash* | |
(message "Flash" *flash*)) | |
body])) | |
(defn index-resource [errors count num] | |
(html-doc "Demo" | |
(when count | |
(message "Count" | |
"The count is currently " (or count 0))) | |
[:h2 "A form"] | |
(form-to [:post "/"] | |
(if-let [msg (:num errors)] | |
[:p {:style "color:red"} msg]) | |
(label :num "Number: ") | |
(text-field :num num) | |
(submit-button "Add")))) | |
(defn parse-int [n] | |
(try (Integer/parseInt n) | |
(catch NumberFormatException _ nil))) | |
(defn assoc-session [response key value] | |
(assoc-in response [:session key] value)) | |
(defn flash [response message] | |
(assoc response :flash 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 (if count | |
(str "Added " n " to " count) | |
(str "Initial count is " n)))) | |
(with-flash "Please fix the error below" | |
(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