Create new HTTP call from Ring Request

169 views Asked by At

I want to 'redirect' web request to another service. I want to change nothing about them and send them to another service (I assume with something like http-clj). The result will then do the same hop.

This is required because we are replacing part of the service, but we can not have the client call service directly.

We deploy into a tomcat but generally it should work for any Ring Request.

Is there a elegant way to do this?

Thanks

3

There are 3 answers

0
ASRye On

The simplest way is probably just returning a 301 Moved Permanently response with the correct Location header.

If you want to actually perform the request you are basically implementing an HTTP proxy and that will probably get complex very fast, and you would be better off using a reverse proxy like Nginx like Scott said

0
Scott On

This isn't a Ring-specific answer, but I feel that you'd be better off using a Reverse Proxy like Nginx in front of both your old web service and the new replacement web service.

A reverse proxy can easily respond to different incoming URLs and transparently pass them through to the correct web service behind the scenes. This is a flexible and powerful approach that will allow for many partitioned URL scheme variations as your service grows.

0
Arthur Ulfeldt On

I have for years done this with cases where a request comes in an it needs to go to two different places. (queue rant about microservices and the law of unintended consequences here) I generally settled on the pattern of using core.async with http-kit.

(handler .....
   (let [output (chan)]
      (http/post bakcend-url (make-new-request-options request)
                 #(do (async/>!! output %)
                      (async/close! output)))
      (go (async/<! (async/timeout 2000))
          (async/>! output {:status 500}))
     ... )