Last active
November 18, 2017 22:39
-
-
Save gustavohenrique/72f087c18f6c062046bf9e5da8bc2f04 to your computer and use it in GitHub Desktop.
A snippet to create a reverse proxy using Go
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("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