Last active
February 18, 2024 14:37
-
-
Save bthuilot/aae91fdb1745edde6713f0142f8cc0d3 to your computer and use it in GitHub Desktop.
Echo Server in Golang
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:latest | |
RUN <<EOF | |
curl -L https://gist.githubusercontent.com/bthuilot/aae91fdb1745edde6713f0142f8cc0d3/raw/echo_server.go > /tmp/echo_server.go | |
go build -o /app/main /tmp/echo_server.go | |
EOF | |
EXPOSE 7890 | |
ENTRYPOINT ["/app/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 ( | |
"log" | |
"net/http" | |
"net/http/httputil" | |
) | |
func main() { | |
http.HandleFunc("/health", func(writer http.ResponseWriter, request *http.Request) { | |
_, _ = writer.Write([]byte("OK")) | |
writer.WriteHeader(200) | |
}) | |
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { | |
log.Println("received request") | |
rawReq, err := httputil.DumpRequest(request, true) | |
if err != nil { | |
log.Println("error dumping request", err) | |
writer.WriteHeader(500) | |
_, _ = writer.Write([]byte("error dumping request")) | |
return | |
} | |
log.Println(string(rawReq)) | |
_, _ = writer.Write(rawReq) | |
}) | |
log.Println("starting echo server...") | |
if err := http.ListenAndServe(":7890", nil); err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To build the docker image from the file without downloading run
curl -L https://gist.githubusercontent.com/bthuilot/aae91fdb1745edde6713f0142f8cc0d3/raw/Dockerfile | docker build -t bthuilot/echo-server:latest -