Skip to content

Instantly share code, notes, and snippets.

@rodrigo-brito
Created October 18, 2018 11:10
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 rodrigo-brito/54143016a05554b4d5562f150931ffd7 to your computer and use it in GitHub Desktop.
Save rodrigo-brito/54143016a05554b4d5562f150931ffd7 to your computer and use it in GitHub Desktop.
Go reverse proxy
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("http://127.0.0.1:3999")
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(">", 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