-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
54 lines (45 loc) · 1.13 KB
/
status.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
package hyper
import (
"bytes"
"net/http"
"os"
"github.com/infiniteloopcloud/log"
)
const (
StatusAsLibrary = "lib"
StatusAsService = "svc"
)
type StatusFn func() string
type StatusOpts struct {
EnvironmentData []string
Statuses map[string]StatusFn
}
func StatusHandler(opts StatusOpts) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
var envData = make(map[string]string)
for _, e := range opts.EnvironmentData {
envData[e] = os.Getenv(e)
}
var status = make(map[string]string)
for name, fn := range opts.Statuses {
status[name] = fn()
}
result := struct {
EnvironmentData map[string]string `json:"environment_data"`
Statuses map[string]string `json:"statuses"`
}{
EnvironmentData: envData,
Statuses: status,
}
resp := new(bytes.Buffer)
err := jsonEncoder.Encode(resp, result)
if err != nil {
log.Debugf(r.Context(), "configDebug handler %s", err.Error())
}
w.WriteHeader(http.StatusOK)
_, err = w.Write(resp.Bytes())
if err != nil {
log.Debugf(r.Context(), "configDebug handler %s", err.Error())
}
}
}