├── Dockerfile ├── README.md └── main.go /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1.14.0 AS builder 2 | 3 | WORKDIR /go/src/github.com/daime/http-dump 4 | 5 | COPY main.go . 6 | 7 | RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o http-dump . 8 | 9 | FROM marcosmorelli/debian-base-image 10 | 11 | WORKDIR /root/ 12 | 13 | COPY --from=0 /go/src/github.com/daime/http-dump/http-dump . 14 | 15 | EXPOSE 8080 16 | 17 | CMD ["./http-dump"] 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # http-dump 2 | 3 | A simple go web server that dumps the request back to the client using 4 | go's [httputil.DumpRequest](https://golang.org/pkg/net/http/httputil/#DumpRequest) 5 | function. It also logs the same http request output to stdout. 6 | 7 | http-dump was created to help debugging communications between docker services 8 | within a docker compose. 9 | 10 | ## Usage 11 | 12 | It's highly recommend to use http-dump docker image. 13 | 14 | ```console 15 | $ docker run --rm -p "8080:8080" -it daime/http-dump:latest 16 | ``` 17 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "net/http" 7 | "net/http/httputil" 8 | "os" 9 | ) 10 | 11 | var port = "8080" 12 | 13 | func main() { 14 | http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 15 | d, err := httputil.DumpRequest(r, true) 16 | if err != nil { 17 | msg := fmt.Sprintf("couldn't dump request: %s", err) 18 | log.Printf(msg) 19 | http.Error(w, msg, http.StatusInternalServerError) 20 | return 21 | } 22 | 23 | b := string(d) 24 | 25 | log.Printf("request received:\n%s\n\n", b) 26 | 27 | if _, err := fmt.Fprintf(w, b); err != nil { 28 | msg := fmt.Sprintf("couldn't write response: %s", err) 29 | log.Printf(msg) 30 | http.Error(w, msg, http.StatusInternalServerError) 31 | return 32 | } 33 | }) 34 | 35 | if p := os.Getenv("PORT"); p != "" { 36 | port = p 37 | } 38 | 39 | addr := ":" + port 40 | 41 | log.Printf("http-dump is listening at %s\n", addr) 42 | log.Fatal(http.ListenAndServe(addr, nil)) 43 | } 44 | --------------------------------------------------------------------------------