Last active
August 29, 2015 14:05
-
-
Save iantruslove/e297320b4052f4278e52 to your computer and use it in GitHub Desktop.
Ring: from the ground up
This file contains 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
(defn wrap-generic-middleware-example [handler & [other-args]] | |
(fn [request] | |
;; Early side-effects: | |
(log/info "Wrapping incoming request...") | |
(let [;; Modify request here: | |
req (assoc request :new-req-key "some value for handlers") | |
;; Run the handlers: | |
resp (handler req)] | |
;; Late side-effects: | |
(log/info "My middleware is almost done.") | |
;; Modify and return the response here: | |
(assoc resp :some-key "some value")))) |
This file contains 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
(defn hello-world-handler [req] | |
{:status 200 | |
:headers {"content-type" "text/plain"} | |
:body (format "Hello, %s!\n" (:remote-addr req))}) | |
(def request {:remote-addr "12.34.56.78" :uri "/" :request-method :get}) | |
(hello-world-handler request) | |
;;=> {:status 200 | |
;; :headers {"content-type" "text/plain"} | |
;; :body "Hello, 12.34.56.78!\n"} |
This file contains 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
(defn wrap-no-cache [handler] | |
(fn [request] | |
(let [response (handler request)] | |
(update-in response [:headers "cache-control"] | |
(fnil identity "no-cache"))))) | |
(def app (wrap-no-cache hello-world-handler)) | |
(app request) | |
;;=> {:status 200 | |
;; :headers {"content-type" "text/plain" | |
;; "cache-control" "no-cache"} | |
;; :body "Hello, 12.34.56.78!\n"} |
This file contains 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
{:foo bar | |
;;TODO!!!!! | |
;;TODO!!!!! | |
} |
This file contains 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
{:status 200 | |
:headers {"Content-Type" "application/json" | |
"Cache-Control" "no-cache"} | |
:body "{\"accountId\":27366391,\"balance\":23.21,\"currency\":\"USD\"}"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment