refactor: move version.go to goutils

This commit is contained in:
yusing
2025-10-09 01:14:43 +08:00
parent 8047067b2b
commit 3aed41e078
10 changed files with 18 additions and 149 deletions

View File

@@ -6,7 +6,7 @@ export GOOS = linux
WEBUI_DIR ?= ../godoxy-frontend
DOCS_DIR ?= ../godoxy-wiki
LDFLAGS = -X github.com/yusing/godoxy/pkg.version=${VERSION} -checklinkname=0
LDFLAGS = -X github.com/yusing/goutils/version.version=${VERSION} -checklinkname=0
ifeq ($(agent), 1)
NAME = godoxy-agent

View File

@@ -9,11 +9,11 @@ import (
"github.com/yusing/godoxy/agent/pkg/env"
"github.com/yusing/godoxy/agent/pkg/server"
"github.com/yusing/godoxy/internal/metrics/systeminfo"
"github.com/yusing/godoxy/pkg"
socketproxy "github.com/yusing/godoxy/socketproxy/pkg"
httpServer "github.com/yusing/goutils/server"
strutils "github.com/yusing/goutils/strings"
"github.com/yusing/goutils/task"
"github.com/yusing/goutils/version"
)
func main() {
@@ -43,7 +43,7 @@ func main() {
log.Fatal().Err(err).Msg("init SSL error")
}
log.Info().Msgf("GoDoxy Agent version %s", pkg.GetVersion())
log.Info().Msgf("GoDoxy Agent version %s", version.Get())
log.Info().Msgf("Agent name: %s", env.AgentName)
log.Info().Msgf("Agent port: %d", env.AgentPort)
log.Info().Msgf("Agent runtime: %s", env.Runtime)

View File

@@ -16,13 +16,13 @@ import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/yusing/godoxy/agent/pkg/certs"
"github.com/yusing/godoxy/pkg"
"github.com/yusing/goutils/version"
)
type AgentConfig struct {
Addr string `json:"addr"`
Name string `json:"name"`
Version pkg.Version `json:"version"`
Version version.Version `json:"version"`
Runtime ContainerRuntime `json:"runtime"`
httpClient *http.Client
@@ -82,7 +82,7 @@ func (cfg *AgentConfig) Parse(addr string) error {
return nil
}
var serverVersion = pkg.GetVersion()
var serverVersion = version.Get()
func (cfg *AgentConfig) StartWithCerts(ctx context.Context, ca, crt, key []byte) error {
clientCert, err := tls.X509KeyPair(crt, key)
@@ -151,7 +151,7 @@ func (cfg *AgentConfig) StartWithCerts(ctx context.Context, ca, crt, key []byte)
return fmt.Errorf("failed to get agent runtime: HTTP %d %s", status, runtimeBytes)
}
cfg.Version = pkg.ParseVersion(string(agentVersionBytes))
cfg.Version = version.Parse(string(agentVersionBytes))
if serverVersion.IsNewerThanMajor(cfg.Version) {
log.Warn().Msgf("agent %s major version mismatch: server: %s, agent: %s", cfg.Name, serverVersion, cfg.Version)

View File

@@ -9,8 +9,8 @@ import (
"github.com/yusing/godoxy/agent/pkg/agent"
"github.com/yusing/godoxy/agent/pkg/env"
"github.com/yusing/godoxy/internal/metrics/systeminfo"
"github.com/yusing/godoxy/pkg"
socketproxy "github.com/yusing/godoxy/socketproxy/pkg"
"github.com/yusing/goutils/version"
)
type ServeMux struct{ *http.ServeMux }
@@ -45,7 +45,7 @@ func NewAgentHandler() http.Handler {
mux.HandleFunc(agent.EndpointProxyHTTP+"/{path...}", ProxyHTTP)
mux.HandleEndpoint("GET", agent.EndpointVersion, func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, pkg.GetVersion())
fmt.Fprint(w, version.Get())
})
mux.HandleEndpoint("GET", agent.EndpointName, func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, env.AgentName)

View File

@@ -16,11 +16,10 @@ import (
"github.com/yusing/godoxy/internal/metrics/systeminfo"
"github.com/yusing/godoxy/internal/metrics/uptime"
"github.com/yusing/godoxy/internal/net/gphttp/middleware"
"github.com/yusing/godoxy/pkg"
gperr "github.com/yusing/goutils/errs"
"github.com/yusing/goutils/server"
"github.com/yusing/goutils/task"
)
"github.com/yusing/goutils/version"
func parallel(fns ...func()) {
var wg sync.WaitGroup
@@ -34,7 +33,7 @@ func main() {
initProfiling()
logging.InitLogger(os.Stderr, memlogger.GetMemLogger())
log.Info().Msgf("GoDoxy version %s", pkg.GetVersion())
log.Info().Msgf("GoDoxy version %s", version.Get())
log.Trace().Msg("trace enabled")
parallel(
dnsproviders.InitProviders,

Submodule goutils updated: c9c0b8d9d0...2717843631

View File

@@ -4,7 +4,7 @@ import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/yusing/godoxy/pkg"
"github.com/yusing/goutils/version"
)
// @x-id "version"
@@ -17,5 +17,5 @@ import (
// @Success 200 {string} string "version"
// @Router /version [get]
func Version(c *gin.Context) {
c.JSON(http.StatusOK, pkg.GetVersion().String())
c.JSON(http.StatusOK, version.Get().String())
}

View File

@@ -15,10 +15,10 @@ import (
"github.com/yusing/godoxy/internal/route/routes"
"github.com/yusing/godoxy/internal/types"
"github.com/yusing/godoxy/internal/watcher/health/monitor"
"github.com/yusing/godoxy/pkg"
gperr "github.com/yusing/goutils/errs"
"github.com/yusing/goutils/http/reverseproxy"
"github.com/yusing/goutils/task"
"github.com/yusing/goutils/version"
)
type ReveseProxyRoute struct {
@@ -74,7 +74,7 @@ func NewReverseProxyRoute(base *Route) (*ReveseProxyRoute, gperr.Error) {
HTTPConfig: httpConfig,
}
setHeaderFunc := cfg.SetAgentProxyConfigHeaders
if !a.Version.IsOlderThan(pkg.Ver(0, 18, 6)) {
if !a.Version.IsOlderThan(version.New(0, 18, 6)) {
setHeaderFunc = cfg.SetAgentProxyConfigHeadersLegacy
}

View File

@@ -8,7 +8,7 @@ import (
"time"
"github.com/yusing/godoxy/internal/types"
"github.com/yusing/godoxy/pkg"
"github.com/yusing/goutils/version"
)
type HTTPHealthMonitor struct {
@@ -52,7 +52,7 @@ func (mon *HTTPHealthMonitor) CheckHealth() (types.HealthCheckResult, error) {
}
req.Close = true
req.Header.Set("Connection", "close")
req.Header.Set("User-Agent", "GoDoxy/"+pkg.GetVersion().String())
req.Header.Set("User-Agent", "GoDoxy/"+version.Get().String())
start := time.Now()
resp, respErr := pinger.Do(req)

View File

@@ -1,130 +0,0 @@
package pkg
import (
"fmt"
"regexp"
"strconv"
"strings"
)
func GetVersion() Version {
return currentVersion
}
func GetLastVersion() Version {
return lastVersion
}
func init() {
currentVersion = ParseVersion(version)
// ignore errors
// versionFile := filepath.Join(common.DataDir, "version")
// var lastVersionStr string
// f, err := os.OpenFile(versionFile, os.O_RDWR|os.O_CREATE, 0o644)
// if err == nil {
// _, err = fmt.Fscanf(f, "%s", &lastVersionStr)
// lastVersion = ParseVersion(lastVersionStr)
// }
// if err != nil && !os.IsNotExist(err) {
// log.Warn().Err(err).Msg("failed to read version file")
// return
// }
// if err := f.Truncate(0); err != nil {
// log.Warn().Err(err).Msg("failed to truncate version file")
// return
// }
// _, err = f.WriteString(version)
// if err != nil {
// log.Warn().Err(err).Msg("failed to save version file")
// return
// }
}
type Version struct{ Generation, Major, Minor int }
func Ver(gen, major, minor int) Version {
return Version{gen, major, minor}
}
func (v Version) String() string {
return fmt.Sprintf("v%d.%d.%d", v.Generation, v.Major, v.Minor)
}
func (v Version) MarshalText() ([]byte, error) {
return []byte(v.String()), nil
}
func (v Version) IsNewerThan(other Version) bool {
if v.Generation != other.Generation {
return v.Generation > other.Generation
}
if v.Major != other.Major {
return v.Major > other.Major
}
return v.Minor > other.Minor
}
func (v Version) IsNewerThanMajor(other Version) bool {
if v.Generation != other.Generation {
return v.Generation > other.Generation
}
return v.Major > other.Major
}
func (v Version) IsOlderThan(other Version) bool {
return !v.IsNewerThan(other)
}
func (v Version) IsOlderThanMajor(other Version) bool {
if v.Generation != other.Generation {
return v.Generation < other.Generation
}
return v.Major < other.Major
}
func (v Version) IsOlderMajorThan(other Version) bool {
return !v.IsNewerThanMajor(other)
}
func (v Version) IsEqual(other Version) bool {
return v.Generation == other.Generation && v.Major == other.Major && v.Minor == other.Minor
}
var (
version = "unset"
currentVersion Version
lastVersion Version
)
var versionRegex = regexp.MustCompile(`^v(\d+)\.(\d+)\.(\d+)(\-\w+)?$`)
func ParseVersion(v string) (ver Version) {
if v == "" {
return ver
}
if !versionRegex.MatchString(v) { // likely feature branch (e.g. feat/some-feature)
return ver
}
v = strings.Split(v, "-")[0]
v = strings.TrimPrefix(v, "v")
parts := strings.Split(v, ".")
if len(parts) != 3 {
return ver
}
gen, err := strconv.Atoi(parts[0])
if err != nil {
return ver
}
major, err := strconv.Atoi(parts[1])
if err != nil {
return ver
}
minor, err := strconv.Atoi(parts[2])
if err != nil {
return ver
}
return Ver(gen, major, minor)
}