Skip to content

Instantly share code, notes, and snippets.

@puma007
Forked from JalfResi/revprox.go
Last active August 29, 2015 14:26
Show Gist options
  • Save puma007/f0a7a027ced72a0f3c9d to your computer and use it in GitHub Desktop.
Save puma007/f0a7a027ced72a0f3c9d 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)
}
proxy := httputil.NewSingleHostReverseProxy(remote)
http.HandleFunc("/", handler(proxy))
err = http.ListenAndServe(":8080", nil)
if err != nil {
panic(err)
}
}
func handler(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
log.Println(r.URL)
w.Header().Set("X-Ben", "Rad")
p.ServeHTTP(w, r)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment