Added option for -v|--verbose, to log file accesses to stdout

This commit is contained in:
Seednode 2022-09-10 16:17:55 -05:00
parent fbd4357afe
commit 469cb96883
3 changed files with 15 additions and 3 deletions

View File

@ -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)
}

View File

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

View File

@ -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
}