Skip to content

Instantly share code, notes, and snippets.

@zehnpaard
Created October 27, 2016 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zehnpaard/74c34c6f61e8adabac1b2b5b1fce1f18 to your computer and use it in GitHub Desktop.
Save zehnpaard/74c34c6f61e8adabac1b2b5b1fce1f18 to your computer and use it in GitHub Desktop.
Simple Ring Demo with Handlers using ring.util.response functions
(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}))
(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