Add webserver dockerfile

This commit is contained in:
Elara 2023-08-01 13:02:55 -07:00
parent 326edb7650
commit ced424ac03
6 changed files with 94 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
webserver/webserver

View File

@ -7,3 +7,8 @@ The containers are stored on my gitea instance, so you can pull them like this:
```bash ```bash
docker pull gitea.elara.ws/elara6331/golang:latest docker pull gitea.elara.ws/elara6331/golang:latest
``` ```
### Images
- `golang`: An alpine-based image with Go installed
- `webserver`: A very simple webserver

4
webserver/Dockerfile Normal file
View File

@ -0,0 +1,4 @@
FROM busybox:latest
COPY webserver /usr/bin/webserver
RUN mkdir /html
ENTRYPOINT webserver

12
webserver/build.sh Executable file
View File

@ -0,0 +1,12 @@
#!/bin/bash
if [[ $# == 0 ]]; then
echo "Usage: $0 <arch>"
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"

61
webserver/cmd/main.go Normal file
View File

@ -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
}

11
webserver/manifest.sh Executable file
View File

@ -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"