Added -d|--debug flag and /_/index endpoint

This commit is contained in:
Seednode 2023-04-28 13:19:07 -05:00
parent 5f3557614d
commit 9cd9c286c2
4 changed files with 38 additions and 1 deletions

View File

@ -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. 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 output
``` ```
Usage: Usage:
@ -96,6 +101,7 @@ Available Commands:
Flags: Flags:
-c, --cache generate directory cache at startup -c, --cache generate directory cache at startup
--cache-file string path to optional persistent cache file --cache-file string path to optional persistent cache file
-d, --debug expose index endpoint
-f, --filter enable filtering -f, --filter enable filtering
-h, --help help for roulette -h, --help help for roulette
-p, --port uint16 port to listen on (default 8080) -p, --port uint16 port to listen on (default 8080)

View File

@ -14,6 +14,7 @@ import (
var ( var (
cache bool cache bool
cacheFile string cacheFile string
debug bool
filtering bool filtering bool
port uint16 port uint16
recursive bool recursive bool
@ -48,6 +49,7 @@ func Execute() {
func init() { func init() {
rootCmd.Flags().BoolVarP(&cache, "cache", "c", false, "generate directory cache at startup") 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().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().BoolVarP(&filtering, "filter", "f", false, "enable filtering")
rootCmd.Flags().Uint16VarP(&port, "port", "p", 8080, "port to listen on") rootCmd.Flags().Uint16VarP(&port, "port", "p", 8080, "port to listen on")
rootCmd.Flags().BoolVarP(&recursive, "recursive", "r", false, "recurse into subdirectories") rootCmd.Flags().BoolVarP(&recursive, "recursive", "r", false, "recurse into subdirectories")

View File

@ -10,7 +10,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var Version = "0.40.0" var Version = "0.41.0"
func init() { func init() {
rootCmd.AddCommand(versionCmd) rootCmd.AddCommand(versionCmd)

View File

@ -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 { func serveStaticFileHandler(paths []string, stats *ServeStats) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
err := serveStaticFile(w, r, paths, stats) err := serveStaticFile(w, r, paths, stats)
@ -834,6 +859,10 @@ func ServePage(args []string) error {
http.Handle("/_/stats", serveStatsHandler(args, stats)) http.Handle("/_/stats", serveStatsHandler(args, stats))
} }
if debug {
http.Handle("/_/index", serveIndexHandler(args, index))
}
err = http.ListenAndServe(":"+strconv.FormatInt(int64(port), 10), nil) err = http.ListenAndServe(":"+strconv.FormatInt(int64(port), 10), nil)
if err != nil { if err != nil {
return err return err