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.
This commit is contained in:
perennial
2024-09-22 14:46:01 +10:00
parent 4329077b91
commit d977ae7d62
5 changed files with 38 additions and 4 deletions
+4 -2
View File
@@ -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..."
+1 -1
View File
@@ -13,7 +13,7 @@
{{- if .Revision != "" }}
<li>
<b>Revision:</b>
<a href="https://codeberg.org/VnPower/PixivFE/commit/{{ .Revision }}">{{ .Revision }}</a><br />
<a href="https://codeberg.org/VnPower/PixivFE/commit/{{ .RevisionHash }}">{{ .Revision }}</a><br />
PixivFE's source code revision
</li>
{{- end }}
+31 -1
View File
@@ -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")
+1
View File
@@ -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,
})
+1
View File
@@ -22,6 +22,7 @@ type Data_about struct {
Time string
Version string
Revision string
RevisionHash string
ImageProxy string
AcceptLanguage string
}