-
Notifications
You must be signed in to change notification settings - Fork 3
/
http.go
77 lines (62 loc) · 1.73 KB
/
http.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
package http
import (
"encoding/json"
"net/http"
"github.com/alexfalkowski/go-health/subscriber"
shttp "github.com/alexfalkowski/go-service/transport/http"
"github.com/alexfalkowski/go-service/version"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"go.uber.org/fx"
)
const (
serving = "SERVING"
notServing = "NOT_SERVING"
)
// RegisterParams health for HTTP.
type RegisterParams struct {
fx.In
Server *shttp.Server
Health *HealthObserver
Liveness *LivenessObserver
Readiness *ReadinessObserver
Version version.Version
}
// Register health for HTTP.
func Register(params RegisterParams) error {
resister("/healthz", params.Server.Mux, params.Health.Observer, params.Version, true)
resister("/livez", params.Server.Mux, params.Liveness.Observer, params.Version, false)
resister("/readyz", params.Server.Mux, params.Readiness.Observer, params.Version, false)
return nil
}
func resister(path string, mux *runtime.ServeMux, ob *subscriber.Observer, version version.Version, withErrors bool) {
mux.HandlePath("GET", path, func(w http.ResponseWriter, r *http.Request, p map[string]string) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Version", string(version))
var (
status int
response string
)
if err := ob.Error(); err != nil {
status = http.StatusServiceUnavailable
response = notServing
} else {
status = http.StatusOK
response = serving
}
w.WriteHeader(status)
data := map[string]any{"status": response}
if withErrors {
errors := map[string]any{}
for n, e := range ob.Errors() {
if e == nil {
continue
}
errors[n] = e.Error()
}
if len(errors) > 0 {
data["errors"] = errors
}
}
_ = json.NewEncoder(w).Encode(data)
})
}