From 45ecc0f85a96d09b4a0ca9839b2598314ad7ac34 Mon Sep 17 00:00:00 2001
From: Will Norris
Date: Wed, 12 Mar 2025 15:00:26 -0700
Subject: [PATCH] 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
---
tsweb/debug.go | 31 ++++++++++++++++++++++++++-----
1 file changed, 26 insertions(+), 5 deletions(-)
diff --git a/tsweb/debug.go b/tsweb/debug.go
index 9e6ce4df4..843324482 100644
--- a/tsweb/debug.go
+++ b/tsweb/debug.go
@@ -34,6 +34,7 @@ type DebugHandler struct {
kvs []func(io.Writer) // output one ... each, see KV()
urls []string // one ... block with link each
sections []func(io.Writer, *http.Request) // invoked in registration order prior to outputting
%s debug
", version.CmdName())
+ f("%s
", html.EscapeString(d.title))
for _, kv := range d.kvs {
kv(w)
}
@@ -103,14 +105,20 @@ func (d *DebugHandler) handle(slug string, handler http.Handler) string {
return href
}
-// Handle registers handler at /debug/ and creates a descriptive
-// entry in /debug/ for it.
+// Handle registers handler at /debug/ and adds a link to it
+// on /debug/ with the provided description.
func (d *DebugHandler) Handle(slug, desc string, handler http.Handler) {
href := d.handle(slug, handler)
d.URL(href, desc)
}
-// HandleSilent registers handler at /debug/. It does not create
+// Handle registers handler at /debug/ 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/. 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.
@@ -118,6 +126,14 @@ func (d *DebugHandler) HandleSilent(slug string, handler http.Handler) {
d.handle(slug, handler)
}
+// HandleSilent registers handler at /debug/. 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/.
func (d *DebugHandler) KV(k string, v any) {
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)
}
+// 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) {
w.Write([]byte("running GC...\n"))
if f, ok := w.(http.Flusher); ok {