-
-
Save sidneyw/2b2a25ffd030738225e64ea8119372b3 to your computer and use it in GitHub Desktop.
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 ( | |
"flag" | |
"log" | |
"net/http" | |
"net/http/httputil" | |
"net/url" | |
"strconv" | |
) | |
func main() { | |
port := flag.Int("port", 8080, "port to listen on") | |
targetURL := flag.String("target-url", "", "downstream service url to proxy to") | |
flag.Parse() | |
u, err := url.Parse(*targetURL) | |
if err != nil { | |
log.Fatalf("Could not parse downstream url: %s", *targetURL) | |
} | |
proxy := httputil.NewSingleHostReverseProxy(u) | |
director := proxy.Director | |
proxy.Director = func(req *http.Request) { | |
director(req) | |
req.Header.Set("X-Forwarded-Host", req.Header.Get("Host")) | |
req.Host = req.URL.Host | |
} | |
http.HandleFunc("/", proxy.ServeHTTP) | |
log.Printf("Listening on port %d", *port) | |
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(*port), nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment