From 0c2bb9562a80dc6ece7368b10fac1bb7917ca1ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Farkas?= Date: Mon, 24 May 2021 10:25:30 +0200 Subject: [PATCH] devenv: slow_proxy_mac: make configurable and smaller (#34560) --- devenv/docker/blocks/slow_proxy_mac/.env | 2 ++ devenv/docker/blocks/slow_proxy_mac/Dockerfile | 9 ++++++--- .../blocks/slow_proxy_mac/docker-compose.yaml | 3 ++- devenv/docker/blocks/slow_proxy_mac/main.go | 17 +++++++++++++---- 4 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 devenv/docker/blocks/slow_proxy_mac/.env diff --git a/devenv/docker/blocks/slow_proxy_mac/.env b/devenv/docker/blocks/slow_proxy_mac/.env new file mode 100644 index 00000000000..74f4e9c1841 --- /dev/null +++ b/devenv/docker/blocks/slow_proxy_mac/.env @@ -0,0 +1,2 @@ +ORIGIN_SERVER=http://host.docker.internal:9090/ +SLEEP_DURATION=60s \ No newline at end of file diff --git a/devenv/docker/blocks/slow_proxy_mac/Dockerfile b/devenv/docker/blocks/slow_proxy_mac/Dockerfile index d48c945f5ab..735d433414c 100644 --- a/devenv/docker/blocks/slow_proxy_mac/Dockerfile +++ b/devenv/docker/blocks/slow_proxy_mac/Dockerfile @@ -1,7 +1,10 @@ - -FROM golang:latest +FROM golang:latest as builder ADD main.go / WORKDIR / -RUN GO111MODULE=off go build -o main . +RUN GO111MODULE=off CGO_ENABLED=0 go build -o main . + +FROM scratch +WORKDIR / EXPOSE 3011 +COPY --from=builder /main /main ENTRYPOINT ["/main"] diff --git a/devenv/docker/blocks/slow_proxy_mac/docker-compose.yaml b/devenv/docker/blocks/slow_proxy_mac/docker-compose.yaml index 47347042df7..135c230c9bc 100644 --- a/devenv/docker/blocks/slow_proxy_mac/docker-compose.yaml +++ b/devenv/docker/blocks/slow_proxy_mac/docker-compose.yaml @@ -3,4 +3,5 @@ ports: - '3011:3011' environment: - ORIGIN_SERVER: 'http://host.docker.internal:9090/' + ORIGIN_SERVER: ${ORIGIN_SERVER} + SLEEP_DURATION: ${SLEEP_DURATION} \ No newline at end of file diff --git a/devenv/docker/blocks/slow_proxy_mac/main.go b/devenv/docker/blocks/slow_proxy_mac/main.go index dece2525c13..4c98eb9d17c 100644 --- a/devenv/docker/blocks/slow_proxy_mac/main.go +++ b/devenv/docker/blocks/slow_proxy_mac/main.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "log" "net/http" "net/http/httputil" @@ -13,16 +12,26 @@ import ( func main() { origin := os.Getenv("ORIGIN_SERVER") if origin == "" { - origin = "http://host.docker.internal:9090/" + // it is never not-set, the default is in the `.env` file + log.Fatalf("missing env-variable ORIGIN_SERVER") } - sleep := time.Minute + sleepDurationStr := os.Getenv("SLEEP_DURATION") + if sleepDurationStr == "" { + // it is never not-set, the default is in the `.env` file + log.Fatalf("missing env-variable SLEEP_DURATION") + } + + sleep, err := time.ParseDuration(sleepDurationStr) + if err != nil { + log.Fatalf("failed to parse SLEEP_DURATION: %v", err) + } originURL, _ := url.Parse(origin) proxy := httputil.NewSingleHostReverseProxy(originURL) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - fmt.Printf("sleeping for %s then proxying request: %s", sleep.String(), r.RequestURI) + log.Printf("sleeping for %s then proxying request: url '%s', headers: '%v'", sleep.String(), r.RequestURI, r.Header) <-time.After(sleep) proxy.ServeHTTP(w, r) })