-
Notifications
You must be signed in to change notification settings - Fork 1
/
version.go
61 lines (55 loc) · 1.32 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
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"runtime/debug"
)
var (
// ldflags
version = "dev"
commit = "dev"
date = "dev"
)
type V struct {
Version string `json:"version"`
Commit string `json:"commit"`
Date string `json:"date"`
BuildInfo debug.BuildInfo `json:"buildInfo"`
}
func VersionInfo(w http.ResponseWriter, r *http.Request) {
// Set the CORS headers to the response.
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set(
"Access-Control-Allow-Methods",
"GET, OPTIONS",
)
w.Header().Set(
"Access-Control-Allow-Headers",
"Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization",
)
if r.Method == http.MethodOptions {
return
}
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
bi, ok := debug.ReadBuildInfo()
if !ok {
_, _ = fmt.Fprintln(os.Stderr, "error reading build-info")
_, _ = fmt.Fprintln(w, "error reading build-info")
}
v := V{
Version: version,
Commit: commit,
Date: date,
BuildInfo: *bi,
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(v); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error encoding version: %v", err)
_, _ = fmt.Fprintf(w, "error encoding version: %v", err)
}
}