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)