From 99ec1d7ebdd5737d3ceb947331142b0428e7bd4d Mon Sep 17 00:00:00 2001 From: Seednode Date: Thu, 14 Sep 2023 00:01:50 -0500 Subject: [PATCH] Allow toggling of page refreshInterval --- cmd/root.go | 19 ++++--------------- cmd/uri.go | 28 +++++++++++++++------------- cmd/web.go | 10 +++++++--- 3 files changed, 26 insertions(+), 31 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 1d506c3..d41aaaa 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -6,13 +6,12 @@ package cmd import ( "log" - "time" "github.com/spf13/cobra" ) const ( - ReleaseVersion string = "0.82.0" + ReleaseVersion string = "0.83.0" ) var ( @@ -33,7 +32,7 @@ var ( Prefix string Profile bool Recursive bool - RefreshInterval string + RefreshInterval bool Russian bool Sorting bool Text bool @@ -45,16 +44,6 @@ var ( Use: "roulette [path]...", Short: "Serves random media from the specified directories.", Args: cobra.MinimumNArgs(1), - PreRunE: func(cmd *cobra.Command, args []string) error { - if RefreshInterval != "" { - interval, err := time.ParseDuration(RefreshInterval) - if err != nil || interval < 500*time.Millisecond { - return ErrIncorrectRefreshInterval - } - } - - return nil - }, RunE: func(cmd *cobra.Command, args []string) error { err := ServePage(args) if err != nil { @@ -88,10 +77,10 @@ func init() { rootCmd.Flags().Uint32Var(&MinimumFileCount, "minimum-files", 1, "skip directories with file counts below this value") rootCmd.Flags().Uint32Var(&PageLength, "page-length", 0, "pagination length for statistics and debug pages") rootCmd.Flags().Uint16VarP(&Port, "port", "p", 8080, "port to listen on") - rootCmd.Flags().StringVar(&Prefix, "prefix", "", "path with which to prefix all listeners (for reverse proxying)") + rootCmd.Flags().StringVar(&Prefix, "prefix", "/", "root path for http handlers (for reverse proxying)") rootCmd.Flags().BoolVar(&Profile, "profile", false, "register net/http/pprof handlers") 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().BoolVar(&RefreshInterval, "refresh-interval", false, "enable automatic page refresh via query parameter") rootCmd.Flags().BoolVar(&Russian, "russian", false, "remove selected images after serving") rootCmd.Flags().BoolVarP(&Sorting, "sort", "s", false, "enable sorting") rootCmd.Flags().BoolVar(&Text, "text", false, "enable support for text files") diff --git a/cmd/uri.go b/cmd/uri.go index 32703c0..ec1d087 100644 --- a/cmd/uri.go +++ b/cmd/uri.go @@ -14,18 +14,12 @@ import ( ) func refreshInterval(r *http.Request) (int64, string) { - var interval string - - if RefreshInterval == "" { - interval = r.URL.Query().Get("refresh") - } else { - interval = RefreshInterval - } + interval := r.URL.Query().Get("refresh") duration, err := time.ParseDuration(interval) switch { - case err != nil || duration == 0: + case err != nil || duration == 0 || !RefreshInterval: return 0, "0ms" case duration < 500*time.Millisecond: return 500, "500ms" @@ -92,12 +86,20 @@ func generateQueryParams(filters *filters, sortOrder, refreshInterval string) st hasParams = true } - if hasParams { - queryParams.WriteString("&") - } - queryParams.WriteString(fmt.Sprintf("refresh=%s", refreshInterval)) + if RefreshInterval { + if hasParams { + queryParams.WriteString("&") + } + queryParams.WriteString(fmt.Sprintf("refresh=%s", refreshInterval)) - return queryParams.String() + hasParams = true + } + + if hasParams { + return queryParams.String() + } + + return "" } func stripQueryParams(request string) (string, error) { diff --git a/cmd/web.go b/cmd/web.go index 26f2741..8530c4a 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -243,7 +243,7 @@ func serveMedia(paths []string, regexes *regexes, formats *types.Types) httprout return } - fileUri := Prefix + "/" + generateFileUri(path) + fileUri := Prefix + generateFileUri(path) fileName := filepath.Base(path) @@ -385,9 +385,13 @@ func ServePage(args []string) error { mux.PanicHandler = serverErrorHandler() - Prefix = strings.TrimSuffix(Prefix, "/") + if !strings.HasSuffix(Prefix, "/") { + Prefix = Prefix + "/" + } - register(mux, Prefix+"/", serveRoot(paths, regexes, cache, formats)) + register(mux, Prefix, serveRoot(paths, regexes, cache, formats)) + + Prefix = strings.TrimSuffix(Prefix, "/") if Prefix != "" { register(mux, "/", redirectRoot())