From ced424ac035c8536a5c353c5db4b85d5b8c0cf20 Mon Sep 17 00:00:00 2001 From: Elara Musayelyan Date: Tue, 1 Aug 2023 13:02:55 -0700 Subject: [PATCH] Add webserver dockerfile --- .gitignore | 1 + README.md | 5 ++++ webserver/Dockerfile | 4 +++ webserver/build.sh | 12 +++++++++ webserver/cmd/main.go | 61 +++++++++++++++++++++++++++++++++++++++++++ webserver/manifest.sh | 11 ++++++++ 6 files changed, 94 insertions(+) create mode 100644 .gitignore create mode 100644 webserver/Dockerfile create mode 100755 webserver/build.sh create mode 100644 webserver/cmd/main.go create mode 100755 webserver/manifest.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fee57cd --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +webserver/webserver diff --git a/README.md b/README.md index 761d72e..c23b608 100644 --- a/README.md +++ b/README.md @@ -7,3 +7,8 @@ The containers are stored on my gitea instance, so you can pull them like this: ```bash docker pull gitea.elara.ws/elara6331/golang:latest ``` + +### Images + +- `golang`: An alpine-based image with Go installed +- `webserver`: A very simple webserver \ No newline at end of file diff --git a/webserver/Dockerfile b/webserver/Dockerfile new file mode 100644 index 0000000..c6472ad --- /dev/null +++ b/webserver/Dockerfile @@ -0,0 +1,4 @@ +FROM busybox:latest +COPY webserver /usr/bin/webserver +RUN mkdir /html +ENTRYPOINT webserver \ No newline at end of file diff --git a/webserver/build.sh b/webserver/build.sh new file mode 100755 index 0000000..6e33f8f --- /dev/null +++ b/webserver/build.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +if [[ $# == 0 ]]; then + echo "Usage: $0 " + exit 1 +fi + +GOOS=linux GOARCH=$1 go build -o webserver ./cmd/main.go + +docker login gitea.elara.ws +docker build . -t "gitea.elara.ws/elara6331/webserver:$1" +docker push "gitea.elara.ws/elara6331/webserver:$1" diff --git a/webserver/cmd/main.go b/webserver/cmd/main.go new file mode 100644 index 0000000..5b51adf --- /dev/null +++ b/webserver/cmd/main.go @@ -0,0 +1,61 @@ +package main + +import ( + "io" + "log" + "net/http" + "os" + "strings" + "time" +) + +func main() { + addr := os.Getenv("WEBSERVER_ADDR") + if addr == "" { + addr = ":8000" + } + log.Println("Starting HTTP server at", addr) + + h := http.FileServer(http.Dir("/html")) + h = methodMiddleware(h) + h = logMiddleware(h) + http.ListenAndServe(addr, h) +} + +func methodMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + w.WriteHeader(http.StatusMethodNotAllowed) + io.WriteString(w, "Method not allowed: "+r.Method) + } else { + next.ServeHTTP(w, r) + } + }) +} + +func logMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + start := time.Now() + next.ServeHTTP(w, r) + log.Printf("[http] Request from %s; latency=%s, method=%s, path=%q\n", getRealIP(r), time.Since(start), r.Method, r.URL.Path) + }) +} + +func getRealIP(r *http.Request) string { + xff := r.Header.Get("X-Forwarded-For") + if xff != "" { + before, _, ok := strings.Cut(xff, ", ") + if ok { + return before + } else { + return xff + } + } + + xrip := r.Header.Get("X-Real-IP") + if xrip != "" { + return xrip + } + + return r.RemoteAddr +} diff --git a/webserver/manifest.sh b/webserver/manifest.sh new file mode 100755 index 0000000..11456c1 --- /dev/null +++ b/webserver/manifest.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +tag="$1" +tag="${tag:=latest}" + +docker login gitea.elara.ws +docker manifest create "gitea.elara.ws/elara6331/webserver:$tag" \ + --amend gitea.elara.ws/elara6331/webserver:amd64 \ + --amend gitea.elara.ws/elara6331/webserver:arm64 \ + --amend gitea.elara.ws/elara6331/webserver:riscv64 +docker manifest push "gitea.elara.ws/elara6331/webserver:$tag"