From 5fdfaee80d3be2a05beb3effbe083d8c50f4cfad Mon Sep 17 00:00:00 2001 From: Seednode Date: Mon, 8 May 2023 09:10:13 -0500 Subject: [PATCH] Split /_/debug endpoint into /_/html and /_/json, formatted accordingly --- README.md | 4 ++-- cmd/version.go | 2 +- cmd/web.go | 46 +++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ac5fab0..2b62af6 100644 --- a/README.md +++ b/README.md @@ -83,9 +83,9 @@ If the `--stats` flag is passed, an additional endpoint, `/_/stats`, is register When accessed, this endpoint returns a JSON document listing every file served, along with the number of times it has been served, its filesize, and timestamps of when it was served. ## Debug -If the `-d|--debug` flag is passed, an additional endpoint, `/_/debug`, is registered. +If the `-d|--debug` flag is passed, two additional endpoints—`/_/html` and `/_/json`—are registered. -When accessed, this endpoint returns a sorted JSON document listing every file currently in the index. This can prove useful when confirming whether the index is generated successfully, or whether a given file is in the index. +When accessed, these endpoints return the contents of the index, in HTML and JSON formats respectively. This can prove useful when confirming whether the index is generated successfully, or whether a given file is in the index. ## Usage output ``` diff --git a/cmd/version.go b/cmd/version.go index e0b91d7..60240f3 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -var Version = "0.42.0" +var Version = "0.43.0" func init() { rootCmd.AddCommand(versionCmd) diff --git a/cmd/web.go b/cmd/web.go index 1e795a1..537777f 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -646,7 +646,46 @@ func serveStatsHandler(args []string, stats *ServeStats) http.HandlerFunc { } } -func serveDebugHandler(args []string, index *Index) http.HandlerFunc { +func serveHtmlDebugHandler(args []string, index *Index) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "text/html") + + startTime := time.Now() + + indexDump := index.Index() + + sort.SliceStable(indexDump, func(p, q int) bool { + return indexDump[p] < indexDump[q] + }) + + var htmlBody strings.Builder + + htmlBody.WriteString(``) + htmlBody.WriteString(``) + htmlBody.WriteString(`Debug`) + for _, v := range indexDump { + htmlBody.WriteString(fmt.Sprintf("%s\n", v, v)) + } + htmlBody.WriteString(``) + + b, err := io.WriteString(w, gohtml.Format(htmlBody.String())) + if err != nil { + return + } + + if verbose { + fmt.Printf("%s | Served HTML debug page (%s) to %s in %s\n", + startTime.Format(LogDate), + humanReadableSize(b), + realIP(r), + time.Since(startTime).Round(time.Microsecond), + ) + } + } +} + +func serveJsonDebugHandler(args []string, index *Index) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/json") @@ -667,7 +706,7 @@ func serveDebugHandler(args []string, index *Index) http.HandlerFunc { w.Write(response) if verbose { - fmt.Printf("%s | Served debug page (%s) to %s in %s\n", + fmt.Printf("%s | Served JSON debug page (%s) to %s in %s\n", startTime.Format(LogDate), humanReadableSize(len(response)), realIP(r), @@ -866,7 +905,8 @@ func ServePage(args []string) error { } if debug { - http.Handle("/_/debug", serveDebugHandler(args, index)) + http.Handle("/_/html", serveHtmlDebugHandler(args, index)) + http.Handle("/_/json", serveJsonDebugHandler(args, index)) } err = http.ListenAndServe(":"+strconv.FormatInt(int64(port), 10), nil)