diff --git a/cmd/files.go b/cmd/files.go index e49318f..62aa526 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -12,6 +12,7 @@ import ( _ "image/gif" _ "image/jpeg" _ "image/png" + "sort" "math/rand" "os" @@ -51,8 +52,8 @@ type ScanStats struct { } type TimesServed struct { - file string - count uint64 + File string + Count uint64 } type ServeStats struct { @@ -66,6 +67,8 @@ func (s *ServeStats) GetFilesTotal() uint64 { } func (s *ServeStats) IncrementCounter(image string) { + s.ImagesServed += 1 + s.ImageCount[image] += 1 if !contains(s.ImageList, image) { @@ -76,11 +79,17 @@ func (s *ServeStats) IncrementCounter(image string) { func (s *ServeStats) ListImages() ([]byte, error) { a := []TimesServed{} + sortedList := s.ImageList + + sort.SliceStable(sortedList, func(p, q int) bool { + return sortedList[p] < sortedList[q] + }) + for _, image := range s.ImageList { a = append(a, TimesServed{image, s.ImageCount[image]}) } - r, err := json.Marshal(a) + r, err := json.MarshalIndent(a, "", " ") if err != nil { return []byte{}, err } diff --git a/cmd/version.go b/cmd/version.go index b2980e3..11bbd45 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -var Version = "0.31.1" +var Version = "0.31.2" func init() { rootCmd.AddCommand(versionCmd) diff --git a/cmd/web.go b/cmd/web.go index 31bfa46..64dfb95 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -307,7 +307,9 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string, sta startTime := time.Now() - stats.IncrementCounter(filePath) + if Debug { + stats.IncrementCounter(filePath) + } buf, err := os.ReadFile(filePath) if err != nil { @@ -342,16 +344,14 @@ func generateCache(args []string, fileCache *[]string) error { func serveCacheClearHandler(args []string, fileCache *[]string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - if Cache { - err := generateCache(args, fileCache) - if err != nil { - fmt.Println(err) - } - - w.WriteHeader(http.StatusOK) - w.Header().Set("Content-Type", "text/plain") - w.Write([]byte("Ok")) + err := generateCache(args, fileCache) + if err != nil { + fmt.Println(err) } + + w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "text/plain") + w.Write([]byte("Ok")) } } @@ -496,13 +496,6 @@ func ServePage(args []string) error { fileCache := &[]string{} - if Cache { - err := generateCache(args, fileCache) - if err != nil { - return err - } - } - stats := &ServeStats{ ImagesServed: 0, ImageList: []string{}, @@ -510,13 +503,22 @@ func ServePage(args []string) error { } http.Handle("/", serveHtmlHandler(paths, regexes, fileCache)) - http.Handle("/clear_cache", serveCacheClearHandler(args, fileCache)) - - http.Handle("/stats", serveStatsHandler(args, stats)) - http.Handle(Prefix+"/", http.StripPrefix(Prefix, serveStaticFileHandler(paths, stats))) http.HandleFunc("/favicon.ico", doNothing) + if Cache { + err := generateCache(args, fileCache) + if err != nil { + return err + } + + http.Handle("/clear_cache", serveCacheClearHandler(args, fileCache)) + } + + if Debug { + http.Handle("/stats", serveStatsHandler(args, stats)) + } + err = http.ListenAndServe(":"+strconv.FormatInt(int64(Port), 10), nil) if err != nil { return err