From 469cb968832a19baa3027dda84732e07b87dbf10 Mon Sep 17 00:00:00 2001 From: Seednode Date: Sat, 10 Sep 2022 16:17:55 -0500 Subject: [PATCH] Added option for -v|--verbose, to log file accesses to stdout --- cmd/root.go | 4 ++-- cmd/version.go | 2 +- cmd/web.go | 12 ++++++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index a17b584..82f84d2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -35,6 +35,6 @@ func Execute() { func init() { rootCmd.Flags().IntVarP(&Port, "port", "p", 8080, "port to listen on") rootCmd.Flags().BoolVarP(&Recursive, "recursive", "r", false, "recurse into subdirectories") - rootCmd.Flags().BoolVarP(&Verbose, "verbose", "v", false, "also write output to stdout") - rootCmd.Flags().SetInterspersed(false) + rootCmd.Flags().BoolVarP(&Verbose, "verbose", "v", false, "log accessed files to stdout") + rootCmd.Flags().SetInterspersed(true) } diff --git a/cmd/version.go b/cmd/version.go index 36e8470..027b143 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -var Version = "0.3.1" +var Version = "0.4.0" func init() { rootCmd.AddCommand(versionCmd) diff --git a/cmd/web.go b/cmd/web.go index 58e5268..fb036a6 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -68,6 +68,10 @@ func serveStaticFile(w http.ResponseWriter, r http.Request, paths []string) erro } if matchesPrefix == false { + if Verbose { + log.Println("Failed to serve file outside specified path(s): " + filePath) + } + http.NotFound(w, &r) return nil @@ -75,6 +79,10 @@ func serveStaticFile(w http.ResponseWriter, r http.Request, paths []string) erro _, err = os.Stat(filePath) if errors.Is(err, os.ErrNotExist) { + if Verbose { + log.Println("Failed to serve non-existent file: " + filePath) + } + http.NotFound(w, &r) return nil @@ -89,6 +97,10 @@ func serveStaticFile(w http.ResponseWriter, r http.Request, paths []string) erro w.Write(buf) + if Verbose { + log.Println("Served file: " + filePath) + } + return nil }