Skip to content

Instantly share code, notes, and snippets.

@gustavohenrique
Last active November 18, 2017 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gustavohenrique/72f087c18f6c062046bf9e5da8bc2f04 to your computer and use it in GitHub Desktop.
Save gustavohenrique/72f087c18f6c062046bf9e5da8bc2f04 to your computer and use it in GitHub Desktop.
A snippet to create a reverse proxy using Go
package main
import (
"fmt"
"net/http"
"net/http/httputil"
"net/url"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
baseURL, err := url.Parse("https://myurl.com")
if err != nil {
panic(err)
}
proxy := httputil.NewSingleHostReverseProxy(&url.URL{
Scheme: baseURL.Scheme,
Host: baseURL.Host,
})
req := r
req.URL.Scheme = baseURL.Scheme
req.URL.Host = baseURL.Host
req.Host = baseURL.Host
fmt.Println("request:", req.URL)
proxy.ServeHTTP(w, req)
})
if err := http.ListenAndServe(":80", nil); err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment