0

Suppose I have the following setup, which has two apps running on different ports using the RestRserve (these might e.g. be on different servers, both of which I control). I would like requests to :8081/health to be forwarded to :8080/health exactly as is, to get the response, and to return the response back to the client. I also need to pass it all the query string variables, etc - the request should look identical, except for the swap of the base URL, so that any request to (say) http://localhost:8081/health is identical to a request to http://localhost:8080/health.

What's the best (simplest/clearest/most robust) way to achieve this?

library(RestRserve)
app1 = Application$new()
app2 = Application$new()
  
app1$add_get(
  path = "/health", 
  FUN = function(.req, .res) {
    .res$set_body("OK 1!")
  })

app2$add_get(
  path = "/health2", 
  FUN = function(.req, .res) {
    .res$set_body("OK 2!")
  })

app2$add_get(
  path = "/health", 
  FUN = function(.req, .res) {
    # Code here to forward request to app1, get result, and then pass back to client
  })

backend = BackendRserve$new()
backend$start(app1, http_port = 8080, background = TRUE)
backend$start(app2, http_port = 8081)

richarddmorey
  • 976
  • 6
  • 19
  • In the most basic step, this is called "redirection" in HTTP. I think two options may be available to you: (1) write `:8081/health` such that it manually does `httr::GET("...:8080/health")` with the called parameters added, a bit brute force imo; or (2) look at changing the [Response](https://restrserve.org/reference/Response.html) (via `.res`) to be HTTP 301 (redirect), not sure how to format the response such that it will be followed by the client. (While most browsers support 301-redirection naturally, some non-interactive client-tools may not.) – r2evans Apr 28 '23 at 12:07
  • This is different from redirection, right? The client need not have direct access to :8080/health; in this setup, the server at :8081 has access to it. If you redirect, then the client needs direct access to :8080, and if it doesn't, the redirect will fail. Suppose, for instance, that on the server, port 8080 isn't open to the internet. – richarddmorey Apr 28 '23 at 12:18
  • Okay, then my first suggestion to use `httr::GET("...:8080/health")` (plus args/params) – r2evans Apr 28 '23 at 12:34
  • Why not use specialized tool for that? nginx/haproxy/..? – Dmitriy Selivanov Apr 28 '23 at 12:43

0 Answers0