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