Added option to force refreshInterval from command line, and added minimum interval of 500ms

This commit is contained in:
Seednode 2023-08-21 15:38:56 -05:00
parent 9b250416d9
commit 6ff79b1817
3 changed files with 49 additions and 25 deletions

View File

@ -62,6 +62,8 @@ If a positive-value `refresh=<integer><unit>` query parameter is provided, the p
This can be used to generate a sort of slideshow of files. This can be used to generate a sort of slideshow of files.
Minimum accepted value is 500ms, as anything lower seems to cause inconsistent behavior. This might be changed in a future release.
Supported units are `ns`, `us`/`µs`, `ms`, `s`, `m`, and `h`. Supported units are `ns`, `us`/`µs`, `ms`, `s`, `m`, and `h`.
## Caching ## Caching
@ -95,19 +97,20 @@ Usage:
roulette <path> [path]... [flags] roulette <path> [path]... [flags]
Flags: Flags:
-b, --bind string address to bind to (default "0.0.0.0") -b, --bind string address to bind to (default "0.0.0.0")
-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 debug endpoint -d, --debug expose debug endpoint
-f, --filter enable filtering -f, --filter enable filtering
-h, --help help for roulette -h, --help help for roulette
--maximum-files uint32 skip directories with file counts over this value (default 4294967295) --maximum-files uint32 skip directories with file counts over this value (default 4294967295)
--minimum-files uint32 skip directories with file counts under this value --minimum-files uint32 skip directories with file counts under this value
-p, --port uint16 port to listen on (default 8080) -p, --port uint16 port to listen on (default 8080)
-r, --recursive recurse into subdirectories -r, --recursive recurse into subdirectories
-s, --sort enable sorting --refresh-interval string force refresh interval equal to this duration (minimum 500ms)
--stats expose stats endpoint -s, --sort enable sorting
--stats-file string path to optional persistent stats file --stats expose stats endpoint
-v, --verbose log accessed files to stdout --stats-file string path to optional persistent stats file
-V, --version display version and exit -v, --verbose log accessed files to stdout
``` -V, --version display version and exit
```

View File

@ -5,13 +5,19 @@ Copyright © 2023 Seednode <seednode@seedno.de>
package cmd package cmd
import ( import (
"errors"
"log" "log"
"time"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var (
ErrIncorrectRefreshInterval = errors.New("refresh interval must be a duration string >= 500ms")
)
const ( const (
Version string = "0.57.1" Version string = "0.57.2"
) )
var ( var (
@ -24,6 +30,7 @@ var (
minimumFileCount uint32 minimumFileCount uint32
port uint16 port uint16
recursive bool recursive bool
refreshInterval string
sorting bool sorting bool
statistics bool statistics bool
statisticsFile string statisticsFile string
@ -38,6 +45,13 @@ var (
if debug { if debug {
cmd.MarkFlagRequired("cache") cmd.MarkFlagRequired("cache")
} }
if refreshInterval != "" {
interval, err := time.ParseDuration(refreshInterval)
if err != nil || interval < 500*time.Millisecond {
log.Fatal(ErrIncorrectRefreshInterval)
}
}
}, },
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
err := ServePage(args) err := ServePage(args)
@ -67,6 +81,7 @@ func init() {
rootCmd.Flags().Uint32Var(&minimumFileCount, "minimum-files", 0, "skip directories with file counts under this value") rootCmd.Flags().Uint32Var(&minimumFileCount, "minimum-files", 0, "skip directories with file counts under this value")
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")
rootCmd.Flags().StringVar(&refreshInterval, "refresh-interval", "", "force refresh interval equal to this duration (minimum 500ms)")
rootCmd.Flags().BoolVarP(&sorting, "sort", "s", false, "enable sorting") rootCmd.Flags().BoolVarP(&sorting, "sort", "s", false, "enable sorting")
rootCmd.Flags().BoolVar(&statistics, "stats", false, "expose stats endpoint") rootCmd.Flags().BoolVar(&statistics, "stats", false, "expose stats endpoint")
rootCmd.Flags().StringVar(&statisticsFile, "stats-file", "", "path to optional persistent stats file") rootCmd.Flags().StringVar(&statisticsFile, "stats-file", "", "path to optional persistent stats file")

View File

@ -408,18 +408,24 @@ func serverErrorHandler() func(http.ResponseWriter, *http.Request, interface{})
} }
func RefreshInterval(r *http.Request, Regexes *Regexes) (int64, string) { func RefreshInterval(r *http.Request, Regexes *Regexes) (int64, string) {
refreshInterval := r.URL.Query().Get("refresh") var interval string
if !Regexes.units.MatchString(refreshInterval) { if refreshInterval == "" {
return 0, "0ms" interval = r.URL.Query().Get("refresh")
} else {
interval = refreshInterval
} }
duration, err := time.ParseDuration(refreshInterval) duration, err := time.ParseDuration(interval)
if err != nil {
return 0, "0ms"
}
return duration.Milliseconds(), refreshInterval switch {
case err != nil || duration == 0:
return 0, "0ms"
case duration < 500*time.Millisecond:
return 500, "500ms"
default:
return duration.Milliseconds(), interval
}
} }
func SortOrder(r *http.Request) string { func SortOrder(r *http.Request) string {