Skip to content

Instantly share code, notes, and snippets.

@sidneyw
Created August 6, 2019 03:45
Show Gist options
  • Save sidneyw/2b2a25ffd030738225e64ea8119372b3 to your computer and use it in GitHub Desktop.
Save sidneyw/2b2a25ffd030738225e64ea8119372b3 to your computer and use it in GitHub Desktop.
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