package handler import ( "net/http" "os" "github.com/gin-gonic/gin" ) // VersionInfo 版本信息 type VersionInfo struct { Version string `json:"version"` BuildTime string `json:"buildTime"` CommitSha string `json:"commitSha"` GoVersion string `json:"goVersion"` } // GetVersion 获取服务器版本信息 func GetVersion(c *gin.Context) { // 从环境变量读取版本信息(由 CI 构建时注入) version := os.Getenv("APP_VERSION") if version == "" { version = "dev" } buildTime := os.Getenv("APP_BUILD_TIME") if buildTime == "" { buildTime = "unknown" } commitSha := os.Getenv("APP_COMMIT_SHA") if commitSha == "" { commitSha = "unknown" } c.JSON(http.StatusOK, VersionInfo{ Version: version, BuildTime: buildTime, CommitSha: commitSha, GoVersion: "go1.22", // 可以通过 runtime.Version() 获取 }) }