Skip to content

Instantly share code, notes, and snippets.

@tonymet
Last active May 9, 2019 16:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonymet/40e97d5d0bb0e79435b9925769bbe8b0 to your computer and use it in GitHub Desktop.
Save tonymet/40e97d5d0bb0e79435b9925769bbe8b0 to your computer and use it in GitHub Desktop.
Dockerfile
main
main
go-http-proxy

Minimal Docker Image with Go HTTP Proxy

FROM golang:stretch AS build
WORKDIR /build
RUN apt-get update && \
apt-get install -y xz-utils
ADD https://github.com/upx/upx/releases/download/v3.95/upx-3.95-amd64_linux.tar.xz /usr/local
RUN xz -d -c /usr/local/upx-3.95-amd64_linux.tar.xz | \
tar -xOf - upx-3.95-amd64_linux/upx > /bin/upx && \
chmod a+x /bin/upx
COPY . .
RUN GO111MODULE=off CGO_ENABLED=0 GOOS=linux \
go build -a -tags netgo -ldflags '-w -s' main.go && \
upx main
FROM scratch
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=build /build/main /main
WORKDIR /
CMD ["/main"]
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
url := r.FormValue("url")
if url == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("\"url\" param is required"))
return
}
resp, err := http.Get(url)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Error fetching url [%s]: %s", url, err.Error())))
return
}
// stream the resp body to our HTTP response, w
writtenCount, err := io.Copy(w, resp.Body)
if err != nil || writtenCount == 0 {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Response was empty from url " + url))
return
}
})
if port := os.Getenv("PORT"); port != "" {
http.ListenAndServe(":"+port, nil)
} else {
log.Panic("PORT not set")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment