ryoakg
3/6/2017 - 1:42 PM

ring-middleware-demo.clj

(set-env! :dependencies '[[ring/ring-core "1.5.0"]
                          [clj-http "2.3.0"]

                          ;; ADAPTERS(https://github.com/ring-clojure/ring/wiki/Third-Party-Libraries#adapters)
                          [ring-simpleweb-adapter "0.2.0"]
                          ;;   ALTERNATIVES
                          ;; [ring/ring-jetty-adapter "1.5.0"]
                          ;; [http-kit "2.2.0"]

                          ;; MIDDLEWARE(https://github.com/ring-clojure/ring/wiki/Third-Party-Libraries#middleware)
                          ])

;;; more info: https://github.com/ring-clojure/ring/wiki

(require 'ring.adapter.simpleweb
         '[clj-http.client :as http]
         '[clojure.pprint :refer [pprint]])


;;; * BASIC
(def conn
  (-> (fn [request]
        (pprint request)
        {:status 200
         :headers {"Content-Type" "text/plain"}
         :body "Hello World"})
      (ring.adapter.simpleweb/run-simpleweb {:port 8080})))

(:body (http/get "http://localhost:8080/"))
;; => "Hello World"
(.close conn)


;;; * QUERY STRING (https://github.com/ring-clojure/ring/wiki/Parameters)
(require 'ring.middleware.params)

(def conn
  (-> (fn [{{:strs [a b]} :query-params
            :as req}]
        (pprint req)
        {:status 200
         :headers {"Content-Type" "text/plain"}
         :body (format "a=%s, b=%s" a b)})
      ring.middleware.params/wrap-params
      (ring.adapter.simpleweb/run-simpleweb {:port 8080})))

(:body (http/get "http://localhost:8080/?a=1&b=2"))
;; => "a=1, b=2"

(.close conn)


;;; * X-WWW-FORM-URLENCODED (https://github.com/ring-clojure/ring/wiki/Parameters)
(require 'ring.middleware.params)

(def conn
  (-> (fn [{{:strs [a b]} :form-params
            :as req}]
        (pprint req)
        {:status 200
         :headers {"Content-Type" "text/plain"}
         :body (format "a=%s, b=%s" a b)})
      ring.middleware.params/wrap-params
      (ring.adapter.simpleweb/run-simpleweb {:port 8080})))

(:body (http/post "http://localhost:8080/"
                  {:form-params {"a" 10 "b" 20}}))
;; => "a=10, b=20"

(.close conn)


;;; * COOKIE (https://github.com/ring-clojure/ring/wiki/Cookies)
(require 'ring.middleware.cookies
         'clj-http.cookies)

(def conn
  (-> (fn [{{{counter :value} "counter"} :cookies
            :as                          req}]
        (pprint req)
        (let [v (or (some-> counter Long/parseLong inc str)
                    "0")]
          {:status  200
           :headers {"Content-Type" "text/plain"}
           :body    v
           :cookies {"counter" {:value   v
                                :path    "/"}}}))
      ring.middleware.cookies/wrap-cookies
      (ring.adapter.simpleweb/run-simpleweb {:port 8080})))

(let [cs (clj-http.cookies/cookie-store)]
  (repeatedly 3 #(:body (http/get "http://localhost:8080/" {:cookie-store cs}))))
;; => ("0" "1" "2")

(.close conn)


;;; * SESSION (https://github.com/ring-clojure/ring/wiki/Sessions)
(require 'ring.middleware.session)
;; (require 'ring.adapter.jetty)

(def conn
  (-> (fn [{{:keys [counter]
             :or   {counter 0}
             :as   session} :session
            :as           req}]
        (pprint req)
        {:status  200
         :headers {"Content-Type" "text/plain"}
         :session (assoc session
                         :counter (inc counter))
         :body    (str counter)})
      ring.middleware.session/wrap-session

      ;; simpleweb cannot seems to be able to work with ring.middleware.session.
      ;; So you should use jetty instead.
      #_(ring.adapter.simpleweb/run-simpleweb {:port 8080})
      (ring.adapter.jetty/run-jetty {:port 8080})))

;; (:body (http/get "http://localhost:8080/"))
;; => "0"
;; => "0"

(.close conn)