-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus_page.go
157 lines (140 loc) · 5.08 KB
/
status_page.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package statuspage
import (
"fmt"
"net/http"
"reflect"
"runtime"
"strconv"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
// Status implements net/http.Handler, and provides a status page for the value returned by cb
type Status[T any] struct {
title string
cb func() T
}
// New constructs a new Status[T] with the passed callback.
func New[T any](title string, cb func() T) *Status[T] {
return &Status[T]{title: title, cb: cb}
}
func (s *Status[T]) ServeHTTP(w http.ResponseWriter, r *http.Request) {
v := s.cb()
rootN, genErr := s.genTopLevelHTML(reflect.ValueOf(v))
if genErr != nil {
http.Error(w, fmt.Sprintf("failed to generate HTML for struct of type %T: %s", v, genErr), 500)
return
}
if renderErr := html.Render(w, rootN); renderErr != nil {
http.Error(w, fmt.Sprintf("failed to render response for struct of type %T: %s", v, genErr), 500)
}
}
func (s *Status[T]) genTopLevelHTML(v reflect.Value) (*html.Node, error) {
root := html.Node{Type: html.DocumentNode}
root.AppendChild(&html.Node{
Type: html.DoctypeNode,
DataAtom: atom.Html,
Data: atom.Html.String(),
})
htmlElem := createElemAtom(atom.Html)
root.AppendChild(htmlElem)
head := createElemAtom(atom.Head)
htmlElem.AppendChild(head)
title := createElemAtom(atom.Title)
title.AppendChild(textNode(s.title))
head.AppendChild(title)
header := createElemAtom(atom.H1)
header.AppendChild(textNode(s.title))
head.AppendChild(header)
body := createElemAtom(atom.Body)
htmlElem.AppendChild(body)
// TODO: add CSS references, etc. to the HEAD element
bodyNodes, bodyGenErr := s.genValSection(v)
if bodyGenErr != nil {
return nil, bodyGenErr
}
for _, bn := range bodyNodes {
// add a horizontal rule to separate sections
body.AppendChild(createElemAtom(atom.Hr))
body.AppendChild(bn)
}
return &root, nil
}
func isNilableType(k reflect.Kind) bool {
switch k {
case reflect.Chan, reflect.Func, reflect.Map, reflect.Pointer,
reflect.UnsafePointer, reflect.Interface, reflect.Slice:
return true
default:
return false
}
}
func (s *Status[T]) genValSection(v reflect.Value) ([]*html.Node, error) {
k := v.Kind()
// If this type implements fmt.Stringer, delegate to that
// implementation as long as the value isn't nil.
if eligibleStringer(v.Type()) && !(isNilableType(k) && v.IsNil()) {
return []*html.Node{textNode(v.Interface().(fmt.Stringer).String())}, nil
}
switch k {
case reflect.Struct:
ns, tblErr := s.genStructTable(v)
if tblErr != nil {
return nil, tblErr
}
return ns, nil
case reflect.Map:
if v.IsNil() {
return []*html.Node{textNode(v.Type().String() + "(nil)")}, nil
}
ns, tblErr := s.genMapTable(v)
if tblErr != nil {
return nil, tblErr
}
return ns, nil
case reflect.Array, reflect.Slice:
return s.genSliceArrayTable(v)
case reflect.Pointer, reflect.Interface:
if v.IsNil() {
return []*html.Node{textNode(v.Type().String() + "(nil)")}, nil
}
// Delegate after following the bouncing ball
return s.genValSection(v.Elem())
case reflect.Bool:
// TODO: wrap this text node in an element node so we can key some CSS styling
return []*html.Node{textNode(strconv.FormatBool(v.Bool()))}, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
// TODO: wrap this text node in an element node so we can key some CSS styling
return []*html.Node{textNode(strconv.FormatInt(v.Int(), 10) + " (0x" + strconv.FormatInt(v.Int(), 16) + ")")}, nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
// TODO: wrap this text node in an element node so we can key some CSS styling
return []*html.Node{textNode(strconv.FormatUint(v.Uint(), 10) + " (0x" + strconv.FormatUint(v.Uint(), 16) + ")")}, nil
case reflect.UnsafePointer:
vp := v.UnsafePointer()
return []*html.Node{textNode(strconv.FormatUint(uint64(uintptr(vp)), 10) + " (0x" + strconv.FormatUint(uint64(uintptr(vp)), 16) + ")")}, nil
case reflect.Float32, reflect.Float64:
// TODO: wrap this text node in an element node so we can key some CSS styling
return []*html.Node{textNode(strconv.FormatFloat(v.Float(), 'g', -1, v.Type().Bits()))}, nil
case reflect.Complex64, reflect.Complex128:
// TODO: wrap this text node in an element node so we can key some CSS styling
return []*html.Node{textNode(strconv.FormatComplex(v.Complex(), 'g', -1, v.Type().Bits()))}, nil
case reflect.String:
return []*html.Node{textNode(v.String())}, nil
case reflect.Chan:
if v.IsNil() {
return []*html.Node{textNode(v.Type().String() + "(nil)")}, nil
}
return []*html.Node{textNode(v.Type().String() + fmt.Sprintf("capacity %d; len %d", v.Cap(), v.Len()))}, nil
case reflect.Func:
return s.genFuncNodes(v)
default:
panic(fmt.Sprintf("unhandled kind %s (type %T)", k, v.Type()))
}
}
func (s *Status[T]) genFuncNodes(v reflect.Value) ([]*html.Node, error) {
if v.IsNil() {
return []*html.Node{textNode(v.Type().String() + "(nil)")}, nil
}
fnPtr := uintptr(v.UnsafePointer())
fn := runtime.FuncForPC(fnPtr)
return []*html.Node{textNode(v.Type().String() + "(0x" + strconv.FormatUint(uint64(fnPtr), 16) + "): " + fn.Name())}, nil
}