-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion.go
46 lines (41 loc) · 886 Bytes
/
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
/*
* Author: Markus Stenberg <[email protected]>
*
* Copyright (c) 2024 Markus Stenberg
*
* Created: Sun Jun 2 10:46:21 2024 mstenber
* Last modified: Sun Jun 2 20:28:01 2024 mstenber
* Edit time: 8 min
*
*/
package main
import (
"encoding/json"
"io"
"net/http"
"runtime/debug"
)
type VersionInfo struct {
BuildTimestamp string
BuildInfo debug.BuildInfo
}
func versionHandler(st State) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
bi, _ := debug.ReadBuildInfo()
w.WriteHeader(http.StatusOK)
if r.FormValue("simple") != "" {
_, _ = io.WriteString(w, st.BuildTimestamp)
return
}
vi := VersionInfo{BuildTimestamp: st.BuildTimestamp}
if bi != nil {
vi.BuildInfo = *bi
}
b, err := json.Marshal(vi)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
_, _ = w.Write(b)
})
}