|
6 | 6 | "fmt" |
7 | 7 | "net" |
8 | 8 | "net/http" |
| 9 | + "net/http/httputil" |
| 10 | + "net/url" |
9 | 11 | "os" |
10 | 12 |
|
11 | 13 | "github.com/buildbuddy-io/buildbuddy/enterprise/server/action_cache_server_proxy" |
@@ -52,10 +54,11 @@ import ( |
52 | 54 | ) |
53 | 55 |
|
54 | 56 | var ( |
55 | | - listen = flag.String("listen", "0.0.0.0", "The interface to listen on (default: 0.0.0.0)") |
56 | | - port = flag.Int("port", 8080, "The port to listen for HTTP traffic on") |
57 | | - sslPort = flag.Int("ssl_port", 8081, "The port to listen for HTTPS traffic on") |
58 | | - monitoringPort = flag.Int("monitoring_port", 9090, "The port to listen for monitoring traffic on") |
| 57 | + listen = flag.String("listen", "0.0.0.0", "The interface to listen on (default: 0.0.0.0)") |
| 58 | + port = flag.Int("port", 8080, "The port to listen for HTTP traffic on") |
| 59 | + sslPort = flag.Int("ssl_port", 8081, "The port to listen for HTTPS traffic on") |
| 60 | + monitoringPort = flag.Int("monitoring_port", 9090, "The port to listen for monitoring traffic on") |
| 61 | + httpProxyTarget = flag.String("http_proxy_target", "", "The HTTP target to forward unknown HTTP requests to.") |
59 | 62 |
|
60 | 63 | serverType = flag.String("server_type", "cache-proxy", "The server type to match on health checks") |
61 | 64 |
|
@@ -135,6 +138,19 @@ func main() { |
135 | 138 | env.GetMux().Handle("/healthz", env.GetHealthChecker().LivenessHandler()) |
136 | 139 | env.GetMux().Handle("/readyz", env.GetHealthChecker().ReadinessHandler()) |
137 | 140 |
|
| 141 | + if *httpProxyTarget != "" { |
| 142 | + httpProxyUrl, err := url.Parse(*httpProxyTarget) |
| 143 | + if err != nil { |
| 144 | + log.Fatalf("Could not parse http proxy url: %v", err) |
| 145 | + } |
| 146 | + proxy := httputil.NewSingleHostReverseProxy(httpProxyUrl) |
| 147 | + |
| 148 | + // Fallback to forward any unmatched HTTP routes to the proxy target. |
| 149 | + env.GetMux().Handle("/", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 150 | + proxy.ServeHTTP(w, req) |
| 151 | + })) |
| 152 | + } |
| 153 | + |
138 | 154 | server := &http.Server{ |
139 | 155 | Addr: fmt.Sprintf("%s:%d", *listen, *port), |
140 | 156 | Handler: env.GetMux(), |
|
0 commit comments