-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
47 lines (41 loc) · 1.27 KB
/
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
46
47
package vars
import (
"expvar"
"fmt"
"net/http"
"github.com/gofiber/adaptor/v2"
"github.com/gofiber/fiber/v2"
g "github.com/maragudk/gomponents"
c "github.com/maragudk/gomponents/components"
h "github.com/maragudk/gomponents/html"
"github.com/pubgo/funk/assert"
"github.com/pubgo/funk/recovery"
"github.com/pubgo/lava/core/debug"
)
func init() {
defer recovery.Exit()
var index = func(keys []string) g.Node {
var nodes []g.Node
nodes = append(nodes, h.H1(g.Text("/expvar")))
nodes = append(nodes, h.A(g.Text("/debug"), g.Attr("href", "/debug")), h.Br())
for i := range keys {
nodes = append(nodes, h.A(g.Text(keys[i]), g.Attr("href", keys[i])), h.Br())
}
return c.HTML5(c.HTML5Props{Title: "/expvar", Body: nodes})
}
debug.Route("/expvar", func(r fiber.Router) {
r.Get("/", adaptor.HTTPHandlerFunc(func(w http.ResponseWriter, request *http.Request) {
var keys []string
expvar.Do(func(kv expvar.KeyValue) {
keys = append(keys, fmt.Sprintf("/debug/expvar/%s", kv.Key))
})
assert.Must(index(keys).Render(w))
}))
r.Get("/:name", func(ctx *fiber.Ctx) error {
var name = ctx.Params("name")
ctx.Response().Header.Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprintln(ctx, expvar.Get(name).String())
return nil
})
})
}