Skip to content

Commit e332c78

Browse files
authored
Support forwarding unknown HTTP requests from the cache proxy (#10293)
Fixes buildbuddy-io/buildbuddy-internal#4898
1 parent 324ace9 commit e332c78

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

enterprise/server/cmd/cache_proxy/cache_proxy.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"fmt"
77
"net"
88
"net/http"
9+
"net/http/httputil"
10+
"net/url"
911
"os"
1012

1113
"github.com/buildbuddy-io/buildbuddy/enterprise/server/action_cache_server_proxy"
@@ -52,10 +54,11 @@ import (
5254
)
5355

5456
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.")
5962

6063
serverType = flag.String("server_type", "cache-proxy", "The server type to match on health checks")
6164

@@ -135,6 +138,19 @@ func main() {
135138
env.GetMux().Handle("/healthz", env.GetHealthChecker().LivenessHandler())
136139
env.GetMux().Handle("/readyz", env.GetHealthChecker().ReadinessHandler())
137140

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+
138154
server := &http.Server{
139155
Addr: fmt.Sprintf("%s:%d", *listen, *port),
140156
Handler: env.GetMux(),

0 commit comments

Comments
 (0)