Added option to force refreshInterval from command line, and added minimum interval of 500ms
This commit is contained in:
parent
9b250416d9
commit
6ff79b1817
|
@ -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
|
||||||
|
@ -105,6 +107,7 @@ Flags:
|
||||||
--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
|
||||||
|
--refresh-interval string force refresh interval equal to this duration (minimum 500ms)
|
||||||
-s, --sort enable sorting
|
-s, --sort enable sorting
|
||||||
--stats expose stats endpoint
|
--stats expose stats endpoint
|
||||||
--stats-file string path to optional persistent stats file
|
--stats-file string path to optional persistent stats file
|
||||||
|
|
17
cmd/root.go
17
cmd/root.go
|
@ -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")
|
||||||
|
|
22
cmd/web.go
22
cmd/web.go
|
@ -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 {
|
||||||
|
|
Loading…
Reference in New Issue