diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..22909d0 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,7 @@ +** +!dist/ +!dist/** +!Dockerfile.dist +!docker/ +!docker/dist-server/ +!docker/dist-server/** diff --git a/Dockerfile.dist b/Dockerfile.dist new file mode 100644 index 0000000..4236d28 --- /dev/null +++ b/Dockerfile.dist @@ -0,0 +1,18 @@ +FROM golang:1.24-alpine AS builder + +WORKDIR /src + +COPY docker/dist-server/ ./ + +RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/dist-server . + +FROM scratch + +WORKDIR /www + +COPY --from=builder /out/dist-server /dist-server +COPY dist/ /www/ + +EXPOSE 80 + +ENTRYPOINT ["/dist-server"] diff --git a/docker/dist-server/go.mod b/docker/dist-server/go.mod new file mode 100644 index 0000000..80bc90b --- /dev/null +++ b/docker/dist-server/go.mod @@ -0,0 +1,3 @@ +module dist-server + +go 1.24 diff --git a/docker/dist-server/main.go b/docker/dist-server/main.go new file mode 100644 index 0000000..adf127d --- /dev/null +++ b/docker/dist-server/main.go @@ -0,0 +1,69 @@ +package main + +import ( + "log" + "net/http" + "os" + "path/filepath" + "strings" +) + +const ( + defaultPort = "80" + webRoot = "/www" + indexName = "index.html" +) + +func main() { + port := strings.TrimSpace(os.Getenv("PORT")) + if port == "" { + port = defaultPort + } + + server := &http.Server{ + Addr: ":" + port, + Handler: http.HandlerFunc(serve), + } + + log.Printf("dist server listening on :%s", port) + if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { + log.Fatal(err) + } +} + +func serve(w http.ResponseWriter, r *http.Request) { + requestPath := strings.TrimPrefix(filepath.Clean("/"+r.URL.Path), "/") + if requestPath == "." || requestPath == "" { + serveIndex(w, r) + return + } + + fullPath := filepath.Join(webRoot, filepath.FromSlash(requestPath)) + if info, err := os.Stat(fullPath); err == nil && !info.IsDir() { + applyAssetCacheHeaders(w, requestPath) + http.ServeFile(w, r, fullPath) + return + } + + serveIndex(w, r) +} + +func serveIndex(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Cache-Control", "no-cache") + w.Header().Set("Pragma", "no-cache") + w.Header().Set("Expires", "0") + http.ServeFile(w, r, filepath.Join(webRoot, indexName)) +} + +func applyAssetCacheHeaders(w http.ResponseWriter, requestPath string) { + if strings.EqualFold(requestPath, indexName) { + w.Header().Set("Cache-Control", "no-cache") + w.Header().Set("Pragma", "no-cache") + w.Header().Set("Expires", "0") + return + } + + if strings.HasPrefix(requestPath, "static/") { + w.Header().Set("Cache-Control", "public, max-age=31536000, immutable") + } +} diff --git a/index.html b/index.html index e76de1c..f13b71e 100644 --- a/index.html +++ b/index.html @@ -8,7 +8,7 @@
- + -->