Skip to content

Instantly share code, notes, and snippets.

@JalfResi
Last active October 25, 2024 15:41
Show Gist options
  • Save JalfResi/6287706 to your computer and use it in GitHub Desktop.
Save JalfResi/6287706 to your computer and use it in GitHub Desktop.
Simple reverse proxy in Go
package main
import(
"log"
"net/url"
"net/http"
"net/http/httputil"
)
func main() {
remote, err := url.Parse("http://google.com")
if err != nil {
panic(err)
}
handler := func(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
log.Println(r.URL)
r.Host = remote.Host
w.Header().Set("X-Ben", "Rad")
p.ServeHTTP(w, r)
}
}
proxy := httputil.NewSingleHostReverseProxy(remote)
http.HandleFunc("/", handler(proxy))
err = http.ListenAndServe(":8080", nil)
if err != nil {
panic(err)
}
}
Copy link

ghost commented Dec 22, 2022

@rjp
Copy link

rjp commented May 24, 2024

Helpful, ta, used this to transparently adjust some query parameters for certain requests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment