-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathversion.go
46 lines (40 loc) · 1.11 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
package version
import (
"encoding/json"
"net/http"
"github.com/adjust/goautoneg"
)
func init() {
json, err := json.Marshal(map[string]string{
"hash": Hash,
"tag": Tag,
"branch": Branch,
"timestamp": Timestamp,
})
if err != nil {
panic(err)
}
jsonOutput = json
}
var (
// Hash is the VCS hash the binary was built from.
Hash string
// Tag is the VCS tag the binary was built from.
Tag string
// Branch is the VCS branch the binary was built from.
Branch string
// Timestamp is the time the binary was built.
Timestamp string
jsonOutput []byte
// Handler is an http.Handler that exposes the versioning
// information as an endpoint.
Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
contentType := goautoneg.Negotiate(r.Header.Get("Accept"), []string{"application/json"})
if contentType == "application/json" {
w.Header().Set("Content-Type", "application/json")
w.Write(jsonOutput)
return
}
w.Write([]byte("VERSION_HASH=\"" + Hash + "\"\nVERSION_TAG=\"" + Tag + "\"\nVERSION_BRANCH=\"" + Branch + "\"\nVERSION_TIME=\"" + Timestamp + "\""))
})
)