juxt / bidi

Bidirectional URI routing
MIT License
991 stars 92 forks source link

Ring async handler support #184

Closed burhanloey closed 6 years ago

burhanloey commented 6 years ago

Hi!

Thank you for this awesome library.

Is there any plan to support Ring async handler for make-handler (or make-async-handler)?

The handler is a 3-arity function as stated here.

burhanloey commented 6 years ago

I guess this is not needed anymore, at least for me.

I ended up with something like this:

(defn- wrap-response [handler]
  (fn
    ([request]
     (compojure.response/render (handler request) request))
    ([request respond raise]
     (compojure.response/send (handler request) request respond raise))))

(defn make-async-handler
  "Create a Ring async handler from the route definition data structure. Matches a
  handler from the uri in the request, and invokes it with the request as a
  parameter."
  ([route]
   (fn [{:keys [uri path-info request-method] :as req} respond raise]
     (let [path (or path-info uri)
           {:keys [handler]} (apply match-route route path req)
           handler' (wrap-response handler)]
       (handler' req respond raise)))))