From d977ae7d62f8a40821ea6a307db28623007cb405 Mon Sep 17 00:00:00 2001 From: perennial Date: Sun, 22 Sep 2024 14:46:01 +1000 Subject: [PATCH] Improve version and revision info handling Modify the REVISION variable to include both commit date and hash. Update config package to parse and store revision components separately. Modify about page and routes to display more detailed revision information. --- Makefile | 6 ++++-- assets/views/about.jet.html | 2 +- config/config.go | 32 +++++++++++++++++++++++++++++++- server/routes/about.go | 1 + server/routes/types.go | 1 + 5 files changed, 38 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 94d56a4..4acaded 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,9 @@ BINARY_NAME=pixivfe TARGETOS ?= $(shell go env GOOS) TARGETARCH ?= $(shell go env GOARCH) -GIT_COMMIT := $(shell git describe --always 2>/dev/null) +GIT_COMMIT_DATE := $(shell git show -s --format=%cd --date=format:"%Y.%m.%d") +GIT_COMMIT_HASH := $(shell git rev-parse --short HEAD) +REVISION := $(GIT_COMMIT_DATE)-$(GIT_COMMIT_HASH) # Include environment variables from .env if it exists -include .env @@ -20,7 +22,7 @@ fmt: build: @echo "Building $(BINARY_NAME)..." go mod download - CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -v -ldflags="-extldflags=-static -X codeberg.org/vnpower/pixivfe/v2/config.REVISION=$(GIT_COMMIT)" -tags netgo -o $(BINARY_NAME) + CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -v -ldflags="-extldflags=-static -X codeberg.org/vnpower/pixivfe/v2/config.REVISION=$(REVISION)" -tags netgo -o $(BINARY_NAME) test: @echo "Running tests..." diff --git a/assets/views/about.jet.html b/assets/views/about.jet.html index 977489b..6f4cc73 100644 --- a/assets/views/about.jet.html +++ b/assets/views/about.jet.html @@ -13,7 +13,7 @@ {{- if .Revision != "" }}
  • Revision: - {{ .Revision }}
    + {{ .Revision }}
    PixivFE's source code revision
  • {{- end }} diff --git a/config/config.go b/config/config.go index 43f338a..db49d12 100644 --- a/config/config.go +++ b/config/config.go @@ -15,11 +15,20 @@ import ( ) var GlobalConfig ServerConfig + +// REVISION stores the current version's revision information var REVISION string = "" +const ( + unknownRevision = "unknown" + revisionFormat = "date-hash" +) + type ServerConfig struct { Version string Revision string + RevisionDate string + RevisionHash string StartingTime string // used in /about page Host string `env:"PIXIVFE_HOST"` @@ -54,10 +63,31 @@ type ServerConfig struct { ResponseSaveLocation string `env:"PIXIVFE_RESPONSE_SAVE_LOCATION,overwrite"` } +// parseRevision extracts date and hash from the revision string +func parseRevision(revision string) (date, hash string) { + if revision == "" { + return unknownRevision, unknownRevision + } + parts := strings.Split(revision, "-") + if len(parts) == 2 { + return parts[0], parts[1] + } + return unknownRevision, revision +} + func (s *ServerConfig) LoadConfig() error { s.Version = "v2.9" s.Revision = REVISION - log.Printf("PixivFE %s %s\n", s.Version, s.Revision) + + s.RevisionDate, s.RevisionHash = parseRevision(REVISION) + + if REVISION == "" { + log.Printf("[WARNING] REVISION is not set. Continuing with unknown revision information.\n") + } else if s.RevisionDate == unknownRevision { + log.Printf("[WARNING] REVISION format is invalid: %s. Expected format '%s'. Continuing with full revision as hash.\n", REVISION, revisionFormat) + } + + log.Printf("PixivFE %s, revision %s\n", s.Version, s.Revision) s.StartingTime = time.Now().UTC().Format("2006-01-02 15:04") diff --git a/server/routes/about.go b/server/routes/about.go index ab21061..901682f 100644 --- a/server/routes/about.go +++ b/server/routes/about.go @@ -10,6 +10,7 @@ func AboutPage(w http.ResponseWriter, r *http.Request) error { Time: config.GlobalConfig.StartingTime, Version: config.GlobalConfig.Version, Revision: config.GlobalConfig.Revision, + RevisionHash: config.GlobalConfig.RevisionHash, ImageProxy: config.GlobalConfig.ProxyServer.String(), AcceptLanguage: config.GlobalConfig.AcceptLanguage, }) diff --git a/server/routes/types.go b/server/routes/types.go index a89a2bd..ecb2f7f 100644 --- a/server/routes/types.go +++ b/server/routes/types.go @@ -22,6 +22,7 @@ type Data_about struct { Time string Version string Revision string + RevisionHash string ImageProxy string AcceptLanguage string }