ring

Shantanu Kumar 2018-01-07T14:46:07.000074Z

Hi. Can anybody point me to a good example for understanding async Ring handlers and middleware? Esp raising exceptions.

Shantanu Kumar 2018-01-07T15:04:16.000013Z

In the middleware, do I need to catch any exceptions and call (raise thrown)?

Shantanu Kumar 2018-01-07T15:08:34.000045Z

For example, in the middleware below:

(defn ping-middleware
  ([handler]
    (ping-middleware handler #{"/ping" "/ping/"}))
  ([handler ping-uris]
    (ping-middleware handler ping-uris (constantly "pong")))
  ([handler ping-uris body-generator]
    (let [ping-uris-set (set ping-uris)
          ping-response (fn [] {:status 200
                                :body (body-generator)
                                :headers {"Content-Type"  "text/plain"
                                          "Cache-Control" "no-store, no-cache, must-revalidate"}})]
      (fn
        ([request]
          (if (->> (:uri request)
                (contains? ping-uris-set))
            (ping-response)
            (handler request)))
        ([request respond raise]
          (if (->> (:uri request)
                (contains? ping-uris-set))
            (respond (ping-response))
            (handler request respond raise)))))))