mirror of
https://github.com/tailscale/tailscale.git
synced 2025-05-01 13:11:01 +00:00
tsweb: add title to DebugHandler and helper registration methods
Allow customizing the title on the debug index page. Also add methods for registering http.HandlerFunc to make it a little easier on callers. Updates tailscale/corp#27058 Change-Id: Ia101a4a3005adb9118051b3416f5a64a4a45987d Signed-off-by: Will Norris <will@tailscale.com>
This commit is contained in:
parent
6d217d81d1
commit
45ecc0f85a
@ -34,6 +34,7 @@ type DebugHandler struct {
|
|||||||
kvs []func(io.Writer) // output one <li>...</li> each, see KV()
|
kvs []func(io.Writer) // output one <li>...</li> each, see KV()
|
||||||
urls []string // one <li>...</li> block with link each
|
urls []string // one <li>...</li> block with link each
|
||||||
sections []func(io.Writer, *http.Request) // invoked in registration order prior to outputting </body>
|
sections []func(io.Writer, *http.Request) // invoked in registration order prior to outputting </body>
|
||||||
|
title string // title displayed on index page
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debugger returns the DebugHandler registered on mux at /debug/,
|
// Debugger returns the DebugHandler registered on mux at /debug/,
|
||||||
@ -45,6 +46,7 @@ func Debugger(mux *http.ServeMux) *DebugHandler {
|
|||||||
}
|
}
|
||||||
ret := &DebugHandler{
|
ret := &DebugHandler{
|
||||||
mux: mux,
|
mux: mux,
|
||||||
|
title: fmt.Sprintf("%s debug", version.CmdName()),
|
||||||
}
|
}
|
||||||
mux.Handle("/debug/", ret)
|
mux.Handle("/debug/", ret)
|
||||||
|
|
||||||
@ -85,7 +87,7 @@ func (d *DebugHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
AddBrowserHeaders(w)
|
AddBrowserHeaders(w)
|
||||||
f := func(format string, args ...any) { fmt.Fprintf(w, format, args...) }
|
f := func(format string, args ...any) { fmt.Fprintf(w, format, args...) }
|
||||||
f("<html><body><h1>%s debug</h1><ul>", version.CmdName())
|
f("<html><body><h1>%s</h1><ul>", html.EscapeString(d.title))
|
||||||
for _, kv := range d.kvs {
|
for _, kv := range d.kvs {
|
||||||
kv(w)
|
kv(w)
|
||||||
}
|
}
|
||||||
@ -103,14 +105,20 @@ func (d *DebugHandler) handle(slug string, handler http.Handler) string {
|
|||||||
return href
|
return href
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle registers handler at /debug/<slug> and creates a descriptive
|
// Handle registers handler at /debug/<slug> and adds a link to it
|
||||||
// entry in /debug/ for it.
|
// on /debug/ with the provided description.
|
||||||
func (d *DebugHandler) Handle(slug, desc string, handler http.Handler) {
|
func (d *DebugHandler) Handle(slug, desc string, handler http.Handler) {
|
||||||
href := d.handle(slug, handler)
|
href := d.handle(slug, handler)
|
||||||
d.URL(href, desc)
|
d.URL(href, desc)
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleSilent registers handler at /debug/<slug>. It does not create
|
// Handle registers handler at /debug/<slug> and adds a link to it
|
||||||
|
// on /debug/ with the provided description.
|
||||||
|
func (d *DebugHandler) HandleFunc(slug, desc string, handler http.HandlerFunc) {
|
||||||
|
d.Handle(slug, desc, handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
// HandleSilent registers handler at /debug/<slug>. It does not add
|
||||||
// a descriptive entry in /debug/ for it. This should be used
|
// a descriptive entry in /debug/ for it. This should be used
|
||||||
// sparingly, for things that need to be registered but would pollute
|
// sparingly, for things that need to be registered but would pollute
|
||||||
// the list of debug links.
|
// the list of debug links.
|
||||||
@ -118,6 +126,14 @@ func (d *DebugHandler) HandleSilent(slug string, handler http.Handler) {
|
|||||||
d.handle(slug, handler)
|
d.handle(slug, handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HandleSilent registers handler at /debug/<slug>. It does not add
|
||||||
|
// a descriptive entry in /debug/ for it. This should be used
|
||||||
|
// sparingly, for things that need to be registered but would pollute
|
||||||
|
// the list of debug links.
|
||||||
|
func (d *DebugHandler) HandleSilentFunc(slug string, handler http.HandlerFunc) {
|
||||||
|
d.HandleSilent(slug, handler)
|
||||||
|
}
|
||||||
|
|
||||||
// KV adds a key/value list item to /debug/.
|
// KV adds a key/value list item to /debug/.
|
||||||
func (d *DebugHandler) KV(k string, v any) {
|
func (d *DebugHandler) KV(k string, v any) {
|
||||||
val := html.EscapeString(fmt.Sprintf("%v", v))
|
val := html.EscapeString(fmt.Sprintf("%v", v))
|
||||||
@ -149,6 +165,11 @@ func (d *DebugHandler) Section(f func(w io.Writer, r *http.Request)) {
|
|||||||
d.sections = append(d.sections, f)
|
d.sections = append(d.sections, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Title sets the title at the top of the debug page.
|
||||||
|
func (d *DebugHandler) Title(title string) {
|
||||||
|
d.title = title
|
||||||
|
}
|
||||||
|
|
||||||
func gcHandler(w http.ResponseWriter, r *http.Request) {
|
func gcHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte("running GC...\n"))
|
w.Write([]byte("running GC...\n"))
|
||||||
if f, ok := w.(http.Flusher); ok {
|
if f, ok := w.(http.Flusher); ok {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user