From 9cd9c286c2ad0cd50ddc929e99394d5429429e34 Mon Sep 17 00:00:00 2001 From: Seednode Date: Fri, 28 Apr 2023 13:19:07 -0500 Subject: [PATCH] Added -d|--debug flag and /_/index endpoint --- README.md | 6 ++++++ cmd/root.go | 2 ++ cmd/version.go | 2 +- cmd/web.go | 29 +++++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0c60d0d..bb59e5f 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,11 @@ 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, `/_/index`, is registered. + +When accessed, this endpoint returns a JSON document listing every file currently in the index. + ## Usage output ``` Usage: @@ -96,6 +101,7 @@ Available Commands: Flags: -c, --cache generate directory cache at startup --cache-file string path to optional persistent cache file + -d, --debug expose index endpoint -f, --filter enable filtering -h, --help help for roulette -p, --port uint16 port to listen on (default 8080) diff --git a/cmd/root.go b/cmd/root.go index 6a089fa..161a9be 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -14,6 +14,7 @@ import ( var ( cache bool cacheFile string + debug bool filtering bool port uint16 recursive bool @@ -48,6 +49,7 @@ func Execute() { func init() { rootCmd.Flags().BoolVarP(&cache, "cache", "c", false, "generate directory cache at startup") rootCmd.Flags().StringVar(&cacheFile, "cache-file", "", "path to optional persistent cache file") + rootCmd.Flags().BoolVarP(&debug, "debug", "d", false, "expose index endpoint") rootCmd.Flags().BoolVarP(&filtering, "filter", "f", false, "enable filtering") rootCmd.Flags().Uint16VarP(&port, "port", "p", 8080, "port to listen on") rootCmd.Flags().BoolVarP(&recursive, "recursive", "r", false, "recurse into subdirectories") diff --git a/cmd/version.go b/cmd/version.go index 6b088e1..6829397 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -var Version = "0.40.0" +var Version = "0.41.0" func init() { rootCmd.AddCommand(versionCmd) diff --git a/cmd/web.go b/cmd/web.go index 0e20b95..4e10e9f 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -646,6 +646,31 @@ func serveStatsHandler(args []string, stats *ServeStats) http.HandlerFunc { } } +func serveIndexHandler(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") + + startTime := time.Now() + + response, err := json.MarshalIndent(index.Index(), "", " ") + if err != nil { + return + } + + w.Write(response) + + if verbose { + fmt.Printf("%s | Served index page (%s) to %s in %s\n", + startTime.Format(LogDate), + humanReadableSize(len(response)), + realIP(r), + time.Since(startTime).Round(time.Microsecond), + ) + } + } +} + func serveStaticFileHandler(paths []string, stats *ServeStats) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { err := serveStaticFile(w, r, paths, stats) @@ -834,6 +859,10 @@ func ServePage(args []string) error { http.Handle("/_/stats", serveStatsHandler(args, stats)) } + if debug { + http.Handle("/_/index", serveIndexHandler(args, index)) + } + err = http.ListenAndServe(":"+strconv.FormatInt(int64(port), 10), nil) if err != nil { return err