|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "runtime" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +// GitCommit stores the latest Git commit hash. |
| 14 | +// Set via -ldflags "-X main.GitCommit=$(git rev-parse HEAD)" |
| 15 | +var GitCommit string |
| 16 | + |
| 17 | +// BuildTime stores the build timestamp in UTC. |
| 18 | +// Set via -ldflags "-X main.BuildTime=$(date -u +%Y-%m-%dT%H:%M:%SZ)" |
| 19 | +var BuildTime string |
| 20 | + |
| 21 | +// Version indicates the version of the binary, such as a release number or semantic version. |
| 22 | +// Set via -ldflags "-X main.Version=v1.0.0" |
| 23 | +var Version string |
| 24 | + |
| 25 | +// GitBranch holds the name of the Git branch from which the build was created. |
| 26 | +// Set via -ldflags "-X main.GitBranch=$(git rev-parse --abbrev-ref HEAD)" |
| 27 | +var GitBranch string |
| 28 | + |
| 29 | +// GitTag represents the most recent Git tag at build time, if any. |
| 30 | +// Set via -ldflags "-X main.GitTag=$(git describe --tags --abbrev=0)" |
| 31 | +var GitTag string |
| 32 | + |
| 33 | +// GitState indicates whether the working directory was "clean" or "dirty" (i.e., with uncommitted changes). |
| 34 | +// Set via -ldflags "-X main.GitState=$(if git diff-index --quiet HEAD --; then echo 'clean'; else echo 'dirty'; fi)" |
| 35 | +var GitState string |
| 36 | + |
| 37 | +// BuildHost stores the hostname of the machine where the binary was built. |
| 38 | +// Set via -ldflags "-X main.BuildHost=$(hostname)" |
| 39 | +var BuildHost string |
| 40 | + |
| 41 | +// GoVersion captures the Go version used to build the binary. |
| 42 | +// Typically, this can be obtained automatically with runtime.Version(), but you can set it manually. |
| 43 | +// Set via -ldflags "-X main.GoVersion=$(go version | awk '{print $3}')" |
| 44 | +var GoVersion string |
| 45 | + |
| 46 | +// BuildUser is the username of the person or system that initiated the build process. |
| 47 | +// Set via -ldflags "-X main.BuildUser=$(whoami)" |
| 48 | +var BuildUser string |
| 49 | + |
| 50 | +var ( |
| 51 | + startTime time.Time |
| 52 | + runtimeHost string |
| 53 | + processName string |
| 54 | +) |
| 55 | + |
| 56 | +// PrintVersionInfo outputs all versioning information for troubleshooting or version checks. |
| 57 | +func PrintVersionInfo() { |
| 58 | + fmt.Printf("Version: %s\n", Version) |
| 59 | + fmt.Printf("Git Commit: %s\n", GitCommit) |
| 60 | + fmt.Printf("Build Time: %s\n", BuildTime) |
| 61 | + fmt.Printf("Git Branch: %s\n", GitBranch) |
| 62 | + fmt.Printf("Git Tag: %s\n", GitTag) |
| 63 | + fmt.Printf("Git State: %s\n", GitState) |
| 64 | + fmt.Printf("Build Host: %s\n", BuildHost) |
| 65 | + fmt.Printf("Go Version: %s\n", GoVersion) |
| 66 | + fmt.Printf("Build User: %s\n", BuildUser) |
| 67 | +} |
| 68 | + |
| 69 | +func init() { |
| 70 | + startTime = time.Now() |
| 71 | + hostname, err := os.Hostname() |
| 72 | + if err == nil { |
| 73 | + runtimeHost = hostname |
| 74 | + } else { |
| 75 | + runtimeHost = "unknown" |
| 76 | + } |
| 77 | + processName = filepath.Base(os.Args[0]) |
| 78 | +} |
| 79 | + |
| 80 | +func VersionInfo() string { |
| 81 | + return fmt.Sprintf("Version: %s, Git Commit: %s, Build Time: %s, Git Branch: %s, Git Tag: %s, Git State: %s, Build Host: %s, Go Version: %s, Build User: %s", |
| 82 | + Version, GitCommit, BuildTime, GitBranch, GitTag, GitState, BuildHost, GoVersion, BuildUser) |
| 83 | +} |
| 84 | + |
| 85 | +func VersionHandler(w http.ResponseWriter, r *http.Request) { |
| 86 | + w.Header().Set("Content-Type", "application/json") |
| 87 | + |
| 88 | + info := VersionInfo() |
| 89 | + uptime := time.Since(startTime).String() |
| 90 | + memstats := &runtime.MemStats{} |
| 91 | + runtime.ReadMemStats(memstats) |
| 92 | + |
| 93 | + response := struct { |
| 94 | + ProcessName string `json:"process_name"` |
| 95 | + BuildInfo string `json:"build_info"` |
| 96 | + Uptime string `json:"uptime"` |
| 97 | + RuntimeHost string `json:"runtime_host"` |
| 98 | + BytesAllocated uint64 `json:"bytes_allocated"` |
| 99 | + }{ |
| 100 | + ProcessName: processName, |
| 101 | + BuildInfo: info, |
| 102 | + Uptime: uptime, |
| 103 | + RuntimeHost: runtimeHost, |
| 104 | + BytesAllocated: memstats.HeapAlloc, |
| 105 | + } |
| 106 | + |
| 107 | + json.NewEncoder(w).Encode(response) |
| 108 | +} |
0 commit comments