-
-
Save thurt/2ae1be5fd12a3501e7f049d96dc68bb9 to your computer and use it in GitHub Desktop.
Simple reverse proxy in Go (forked from original to use a struct instead of a closure)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
// use http.Handle instead of http.HandleFunc when your struct implements http.Handler interface | |
http.Handle("/", &ProxyHandler{proxy}) | |
err = http.ListenAndServe(":8080", nil) | |
if err != nil { | |
panic(err) | |
} | |
} | |
type ProxyHandler struct { | |
p *httputil.ReverseProxy | |
} | |
func (ph *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
log.Println(r.URL) | |
w.Header().Set("X-Ben", "Rad") | |
ph.p.ServeHTTP(w, r) | |
} |
thanks :)
you are missing the r.Host = remote.Host
part from original gist
Great! 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is why github is awesome - top effort and lovely code! Thanks :)