Skip to content

Instantly share code, notes, and snippets.

@stevehenderson
Forked from JalfResi/revprox.go
Created March 11, 2022 15:22
Show Gist options
  • Save stevehenderson/6cf026179bd88e134ec6add46efffbaf to your computer and use it in GitHub Desktop.
Save stevehenderson/6cf026179bd88e134ec6add46efffbaf 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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment