-
-
Save tonymet/40e97d5d0bb0e79435b9925769bbe8b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Dockerfile | |
main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main | |
go-http-proxy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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