Created
October 27, 2016 14:12
-
-
Save zehnpaard/74c34c6f61e8adabac1b2b5b1fce1f18 to your computer and use it in GitHub Desktop.
Simple Ring Demo with Handlers using ring.util.response functions
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
(ns ring-with-handlers.core | |
(require | |
[ring.util.response :as res] | |
[ring.adapter.jetty :refer [run-jetty]] | |
)) | |
(defn plain-text [req] | |
(-> (res/response "Hello Goodbye") | |
(res/content-type "text/plain"))) | |
(defn html [req] | |
(-> (res/response | |
"<div> | |
<h1>Hello World!</h1> | |
<p>Some random bit of text</p> | |
</div>") | |
(res/content-type "text/html"))) | |
(defn not-found [req] | |
(-> (res/not-found "<b>Not found!</b>") | |
(res/content-type "text/html"))) | |
(defn redirect [req] | |
(res/redirect "http://www.google.com")) | |
(defonce log-atom (atom [])) | |
(defn logger [handler] | |
(fn [req] | |
(let [res (handler req)] | |
(swap! log-atom #(conj % {:request req :response res})) | |
res))) | |
(def my-app | |
(-> plain-text ;swap this with another handler function | |
logger)) | |
(defonce server (run-jetty #'my-app {:port 8080 :join? false})) |
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
(defproject ring-with-handlers "0.1.0-SNAPSHOT" | |
:dependencies [[org.clojure/clojure "1.8.0"] | |
[ring "1.5.0"]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment