diff --git a/Dockerfile b/Dockerfile index 284439e..e79e6bf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,7 +9,11 @@ COPY go.mod go.sum ./ RUN go mod download COPY . . -RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o /shop ./cmd/shop +ARG APP_VERSION=dev +ARG BUILD_TIME=unknown +RUN CGO_ENABLED=0 GOOS=linux go build \ + -ldflags="-s -w -X shop/internal/version.Version=${APP_VERSION}" \ + -o /shop ./cmd/shop # Runtime stage FROM alpine:3.21 diff --git a/cmd/shop/main.go b/cmd/shop/main.go index 0fbec0f..18fe0ea 100644 --- a/cmd/shop/main.go +++ b/cmd/shop/main.go @@ -16,6 +16,7 @@ import ( "shop/internal/middleware" "shop/internal/repository" "shop/internal/upload" + "shop/internal/version" ) func main() { @@ -107,7 +108,7 @@ func main() { } go func() { - log.Printf("server listening on %s", addr) + log.Printf("Shop %s listening on %s", version.Version, addr) if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { log.Fatalf("listen: %v", err) } diff --git a/docker-compose.yml b/docker-compose.yml index 9be6301..e5c3217 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -26,6 +26,9 @@ services: build: context: . dockerfile: Dockerfile + args: + APP_VERSION: ${APP_VERSION:-dev} + BUILD_TIME: ${BUILD_TIME:-} container_name: shop-app restart: unless-stopped env_file: .env @@ -34,6 +37,7 @@ services: DB_PORT: "5432" APP_PORT: "8080" UPLOAD_DIR: /app/data/uploads + APP_VERSION: ${APP_VERSION:-dev} volumes: - uploads:/app/data/uploads depends_on: diff --git a/internal/handlers/customer.go b/internal/handlers/customer.go index 067381e..c540ac3 100644 --- a/internal/handlers/customer.go +++ b/internal/handlers/customer.go @@ -1,6 +1,7 @@ package handlers import ( + "fmt" "html/template" "log" "net/http" @@ -9,6 +10,7 @@ import ( "shop/internal/auth" "shop/internal/models" "shop/internal/repository" + "shop/internal/version" ) type CustomerHandler struct { @@ -26,6 +28,7 @@ type shopPage struct { Title string User *models.User CartCount int + Version string Products interface{} Categories interface{} Error string @@ -73,13 +76,14 @@ func NewCustomerHandler( func (h *CustomerHandler) render(w http.ResponseWriter, name string, data any) { w.Header().Set("Content-Type", "text/html; charset=utf-8") + w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") if err := h.templates.ExecuteTemplate(w, name, data); err != nil { log.Printf("render %s: %v", name, err) } } func (h *CustomerHandler) basePage(r *http.Request, w http.ResponseWriter, title string) shopPage { - p := shopPage{Title: title} + p := shopPage{Title: title, Version: version.Version} if uid, ok := auth.UserIDFromSession(h.userSession, r); ok { if u, err := h.users.GetByID(r.Context(), uid); err == nil { p.User = &u @@ -373,5 +377,6 @@ func (h *CustomerHandler) Account(w http.ResponseWriter, r *http.Request) { func (h *CustomerHandler) Health(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") - w.Write([]byte(`{"status":"ok"}`)) + w.Header().Set("Cache-Control", "no-store") + fmt.Fprintf(w, `{"status":"ok","version":%q}`, version.Version) } diff --git a/internal/handlers/templates/home.html b/internal/handlers/templates/home.html index d2e47dd..8bcd859 100644 --- a/internal/handlers/templates/home.html +++ b/internal/handlers/templates/home.html @@ -118,7 +118,7 @@