-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
45 lines (38 loc) · 978 Bytes
/
debug.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
package healthy
import (
"net/http"
"time"
jjson "github.com/goccy/go-json"
"github.com/gofiber/fiber/v2"
"github.com/pubgo/funk/try"
"github.com/pubgo/lava/core/debug"
"github.com/pubgo/lava/core/healthy"
)
func init() {
debug.Get("/health", func(ctx *fiber.Ctx) error {
dt := make(map[string]*health)
for _, name := range healthy.List() {
h := &health{}
h.Err = try.Try(func() error {
defer func(s time.Time) { h.Cost = time.Since(s).String() }(time.Now())
return healthy.Get(name)(ctx)
})
dt[name] = h
}
bts, err := jjson.Marshal(dt)
if err != nil {
ctx.Status(http.StatusInternalServerError)
_, err = ctx.Write([]byte(err.Error()))
return err
}
ctx.Response().Header.Set("content-type", "application/json")
ctx.Status(http.StatusOK)
_, err = ctx.Write(bts)
return err
})
}
type health struct {
Cost string `json:"cost,omitempty"`
Err error `json:"err,omitempty"`
Msg string `json:"err_msg,omitempty"`
}