diff --git a/enterprise/server/cmd/cache_proxy/cache_proxy.go b/enterprise/server/cmd/cache_proxy/cache_proxy.go index 0b593846c3e..b149f360289 100644 --- a/enterprise/server/cmd/cache_proxy/cache_proxy.go +++ b/enterprise/server/cmd/cache_proxy/cache_proxy.go @@ -5,6 +5,8 @@ import ( "flag" "fmt" "net/http" + "net/http/httputil" + "net/url" "os" "github.com/buildbuddy-io/buildbuddy/enterprise/server/action_cache_server_proxy" @@ -49,10 +51,11 @@ import ( ) var ( - listen = flag.String("listen", "0.0.0.0", "The interface to listen on (default: 0.0.0.0)") - port = flag.Int("port", 8080, "The port to listen for HTTP traffic on") - sslPort = flag.Int("ssl_port", 8081, "The port to listen for HTTPS traffic on") - monitoringPort = flag.Int("monitoring_port", 9090, "The port to listen for monitoring traffic on") + listen = flag.String("listen", "0.0.0.0", "The interface to listen on (default: 0.0.0.0)") + port = flag.Int("port", 8080, "The port to listen for HTTP traffic on") + sslPort = flag.Int("ssl_port", 8081, "The port to listen for HTTPS traffic on") + monitoringPort = flag.Int("monitoring_port", 9090, "The port to listen for monitoring traffic on") + httpProxyTarget = flag.String("http_proxy_target", "", "The HTTP target to forward unknown HTTP requests to.") serverType = flag.String("server_type", "cache-proxy", "The server type to match on health checks") @@ -129,6 +132,19 @@ func main() { env.GetMux().Handle("/healthz", env.GetHealthChecker().LivenessHandler()) env.GetMux().Handle("/readyz", env.GetHealthChecker().ReadinessHandler()) + if *httpProxyTarget != "" { + httpProxyUrl, err := url.Parse(*httpProxyTarget) + if err != nil { + log.Fatalf("Could not parse http proxy url: %v", err) + } + proxy := httputil.NewSingleHostReverseProxy(httpProxyUrl) + + // Fallback to forward any unmatched HTTP routes to the proxy target. + env.GetMux().Handle("/", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + proxy.ServeHTTP(w, req) + })) + } + server := &http.Server{ Addr: fmt.Sprintf("%s:%d", *listen, *port), Handler: env.GetMux(),