0

I want to build a golang service which will listen for GET request, do some URL manipulation and then proxy a new request (to the manipulated URL) back to the browser:

(from browser -> server) GET http://www.example.com/7fbsjfhfh93hdkwhfbf398fhkef93..

(server manipulates URL - decrypts "7fbsjfhfh93hdkwhfbf398fhkef93.." -> "my-super-resource")

(server -> URL resource) GET http://www.somewhereelse.com/my-super-resource

(server -> browser) Response from http://www.somewhereelse.com/my-super-resource passed on to browser (using cors)

The whole chain will need to be synchronous which is ok. Is there a decent proxy library which allows for this sort of thing?

2
  • 2
    Does this not work with the std library ReverseProxy? Commented Jun 15, 2018 at 15:18
  • You could also just use a standard http.HandlerFunc, receive a request, do something to the request, then make another request with a package like Sling, wait for the response, and use that response to send the response back to the original request. That gives you the proxy behavior as well and doesn't depend too much on any sort of a "framework" approach. Plus, I highly doubt you'll find anything that will allow you to do exactly what you detailed with precision. That is very specific...you will likely need to write some custom code as explained above. Commented Jun 15, 2018 at 15:29

1 Answer 1

1

You can do something like this in less than 10 lines of code with the Sling package:

type Foo struct {
    Bar string `json:"bar"`
}

func Handler(w http.ResponseWriter, r *http.Request) {
    // Get the URL and decrypt it
    url := getUrl(r)
    decryptedUrl := decryptUrl(url)

    // Use the decrypted URL to make a request to another service
    var data *Foo
    req, err := sling.New().Get(decryptedUrl).Receive(data)
    if err != nil {
         // Handle error...
    }

    // Respond to the original request using the data from the other service
    respond(w, http.StatusOK, data)
}
Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't have reverse proxy semantics, of course. For starters, interesting request headers and none of the response headers are passed along.
True. However it was more of a blueprint for how he could achieve similar functionality. He can customize it more and pass along headers, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.