I’m thinking about creating something like this.
proxy := &httputil.ReverseProxy{
Director: director,
Transport: &proxyTransport{},
}
server := http.Server{
Addr: ":6060",
Handler: proxy,
}
server.ListenAndServe()
And the function func director(r *http.Request){}
will change the URL of the request and the custom http.Transport
will get the body of the upstream response, does the necessary modifications and sends down a new response.
func director(r *http.Request){
r.URL = url.Parse("foo.bar")
}
.
type proxyTransport struct {
}
func (p *proxyTransport) RoundTrip(req *http.Request) (*http.Response, error) {
res, err := trans.RoundTrip(req)
if err != nil {
return nil, err
}
body, err := httputil.DumpResponse(res, true)
if err != nil {
return nil, err
}
nbody := &bytes.Buffer{}
nbody.Write(append(body, []byte("foo")...))
b := ioutil.NopCloser(nbody)
nres := &http.Response{
StatusCode: http.StatusOK,
Status: fmt.Sprintf("%d %s", http.StatusOK, http.StatusText(http.StatusOK)),
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: http.Header{},
Body: b,
Request: req,
}
nres.Body.Close()
return nres, nil
}
Is this the correct way to go? What other things I need to take care of to not to screw up the performance?