Fork me on GitHub
#ring
<
2018-01-07
>
Shantanu Kumar14:01:07

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

Shantanu Kumar15:01:16

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

Shantanu Kumar15:01:34

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)))))))