-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathversion.go
112 lines (93 loc) · 2.36 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"encoding/json"
"fmt"
"net/http"
"runtime"
"time"
)
type Tag struct {
Name string `json:"name"`
Commit struct {
SHA string `json:"sha"`
URL string `json:"url"`
} `json:"commit"`
}
type Commit struct {
SHA string `json:"sha"`
}
var (
// Populated by the Go linker during build
version = "unknown"
gitCommit = "unknown"
gitBranch = "unknown"
buildDate = "unknown"
)
func versionHandler(w http.ResponseWriter, r *http.Request) {
set_secure_headers(w, r)
set_no_cache(w)
latestCommit := "unknown"
latestCommitObj := getLatestCommitFromBranch(gitBranch)
if latestCommitObj != nil {
latestCommit = latestCommitObj.SHA
}
err := json.NewEncoder(w).Encode(map[string]string{
"current_git_commit": gitCommit,
"git_branch": gitBranch,
"latest_git_commit": latestCommit,
})
if err != nil {
fmt.Println("JSON encode error: ", err)
}
}
func PrintVersion() {
fmt.Printf("Version: %s\nGit Commit: %s\nGit Branch: %s\nGo Version: %s\nGo OS/Arch: %s/%s\nBuild Date: %s\nTime: %s\n",
version, gitCommit, gitBranch, runtime.Version(), runtime.GOOS, runtime.GOARCH, buildDate, time.Now().Format("2006-01-02 15:04:05 MST-07:00"))
}
// func getLatestGit() *Tag {
// url := "https://api.github.com/repos/adamjsturge/xsshunter-go/tags"
// resp, err := http.Get(url)
// if err != nil {
// return nil
// }
// defer resp.Body.Close()
// if resp.StatusCode != http.StatusOK {
// fmt.Printf("error fetching tags: %s", resp.Status)
// return nil
// }
// var tags []Tag
// err = json.NewDecoder(resp.Body).Decode(&tags)
// if err != nil {
// fmt.Printf("error decoding response: %v", err)
// return nil
// }
// // sort.Slice(tags, func(i, j int) bool {
// // return tags[i].Name > tags[j].Name
// // })
// if len(tags) > 0 {
// return &tags[0]
// }
// return nil
// }
func getLatestCommitFromBranch(branch string) *Commit {
if branch == "unknown" {
return nil
}
url := "https://api.github.com/repos/adamjsturge/xsshunter-go/commits/" + branch
resp, err := http.Get(url) // #nosec G107
if err != nil {
return nil
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Printf("error fetching commit: %s", resp.Status)
return nil
}
var commit Commit
err = json.NewDecoder(resp.Body).Decode(&commit)
if err != nil {
fmt.Printf("error decoding response: %v", err)
return nil
}
return &commit
}