devenv: slow_proxy_mac: make configurable and smaller (#34560)

This commit is contained in:
Gábor Farkas 2021-05-24 10:25:30 +02:00 committed by GitHub
parent 68513b9a3f
commit 0c2bb9562a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 8 deletions

View File

@ -0,0 +1,2 @@
ORIGIN_SERVER=http://host.docker.internal:9090/
SLEEP_DURATION=60s

View File

@ -1,7 +1,10 @@
FROM golang:latest as builder
FROM golang:latest
ADD main.go / ADD main.go /
WORKDIR / WORKDIR /
RUN GO111MODULE=off go build -o main . RUN GO111MODULE=off CGO_ENABLED=0 go build -o main .
FROM scratch
WORKDIR /
EXPOSE 3011 EXPOSE 3011
COPY --from=builder /main /main
ENTRYPOINT ["/main"] ENTRYPOINT ["/main"]

View File

@ -3,4 +3,5 @@
ports: ports:
- '3011:3011' - '3011:3011'
environment: environment:
ORIGIN_SERVER: 'http://host.docker.internal:9090/' ORIGIN_SERVER: ${ORIGIN_SERVER}
SLEEP_DURATION: ${SLEEP_DURATION}

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"fmt"
"log" "log"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
@ -13,16 +12,26 @@ import (
func main() { func main() {
origin := os.Getenv("ORIGIN_SERVER") origin := os.Getenv("ORIGIN_SERVER")
if origin == "" { 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) originURL, _ := url.Parse(origin)
proxy := httputil.NewSingleHostReverseProxy(originURL) proxy := httputil.NewSingleHostReverseProxy(originURL)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 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) <-time.After(sleep)
proxy.ServeHTTP(w, r) proxy.ServeHTTP(w, r)
}) })