Added support for time.Duration refresh intervals, instead of just integer seconds
This commit is contained in:
parent
701d0c2666
commit
d343be7253
|
@ -58,10 +58,12 @@ Note: These patterns require sequentially-numbered files matching the following
|
||||||
|
|
||||||
## Refresh
|
## Refresh
|
||||||
|
|
||||||
If a non-zero `refresh=<time in seconds>` query parameter is provided, the page will reload after that interval.
|
If a non-zero `refresh=<integer><unit>` query parameter is provided, the page will reload after that interval.
|
||||||
|
|
||||||
This can be used to generate a sort of slideshow of images.
|
This can be used to generate a sort of slideshow of images.
|
||||||
|
|
||||||
|
Supported units are `ns`, `us`/`µs`, `ms`, `s`, `m`, and `h`.
|
||||||
|
|
||||||
## Usage output
|
## Usage output
|
||||||
```
|
```
|
||||||
Usage:
|
Usage:
|
||||||
|
|
|
@ -10,7 +10,7 @@ import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Version = "0.28.3"
|
var Version = "0.29.0"
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(versionCmd)
|
rootCmd.AddCommand(versionCmd)
|
||||||
|
|
35
cmd/web.go
35
cmd/web.go
|
@ -31,6 +31,7 @@ const (
|
||||||
type Regexes struct {
|
type Regexes struct {
|
||||||
Alphanumeric *regexp.Regexp
|
Alphanumeric *regexp.Regexp
|
||||||
Filename *regexp.Regexp
|
Filename *regexp.Regexp
|
||||||
|
Units *regexp.Regexp
|
||||||
}
|
}
|
||||||
|
|
||||||
type Filters struct {
|
type Filters struct {
|
||||||
|
@ -86,16 +87,21 @@ func notFound(w http.ResponseWriter, r *http.Request, filePath string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getRefreshInterval(r *http.Request) string {
|
func getRefreshInterval(r *http.Request, regexes *Regexes) (int64, string) {
|
||||||
refreshInterval := r.URL.Query().Get("refresh")
|
refreshInterval := r.URL.Query().Get("refresh")
|
||||||
|
|
||||||
num, err := strconv.Atoi(refreshInterval)
|
if !regexes.Units.MatchString(refreshInterval) {
|
||||||
|
return 0, "0ms"
|
||||||
if err != nil || num < 0 {
|
|
||||||
refreshInterval = "0"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return refreshInterval
|
duration, err := time.ParseDuration(refreshInterval)
|
||||||
|
if err != nil {
|
||||||
|
return 0, "0ms"
|
||||||
|
}
|
||||||
|
|
||||||
|
durationInMs := duration.Milliseconds()
|
||||||
|
|
||||||
|
return durationInMs, refreshInterval
|
||||||
}
|
}
|
||||||
|
|
||||||
func getSortOrder(r *http.Request) string {
|
func getSortOrder(r *http.Request) string {
|
||||||
|
@ -228,14 +234,14 @@ func getRealIp(r *http.Request) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func serveHtml(w http.ResponseWriter, r *http.Request, filePath string, dimensions *Dimensions, filters *Filters) error {
|
func serveHtml(w http.ResponseWriter, r *http.Request, filePath string, dimensions *Dimensions, filters *Filters, regexes *Regexes) error {
|
||||||
fileName := filepath.Base(filePath)
|
fileName := filepath.Base(filePath)
|
||||||
|
|
||||||
w.Header().Add("Content-Type", "text/html")
|
w.Header().Add("Content-Type", "text/html")
|
||||||
|
|
||||||
sortOrder := getSortOrder(r)
|
sortOrder := getSortOrder(r)
|
||||||
|
|
||||||
refreshInterval := getRefreshInterval(r)
|
refreshTimer, refreshInterval := getRefreshInterval(r, regexes)
|
||||||
|
|
||||||
queryParams := generateQueryParams(filters, sortOrder, refreshInterval)
|
queryParams := generateQueryParams(filters, sortOrder, refreshInterval)
|
||||||
|
|
||||||
|
@ -250,13 +256,7 @@ func serveHtml(w http.ResponseWriter, r *http.Request, filePath string, dimensio
|
||||||
dimensions.Width,
|
dimensions.Width,
|
||||||
dimensions.Height))
|
dimensions.Height))
|
||||||
htmlBody.WriteString(`</head><body>`)
|
htmlBody.WriteString(`</head><body>`)
|
||||||
if refreshInterval != "0" {
|
if refreshInterval != "0ms" {
|
||||||
r, err := strconv.Atoi(refreshInterval)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
refreshTimer := strconv.Itoa(r * 1000)
|
|
||||||
|
|
||||||
htmlBody.WriteString(fmt.Sprintf("<script>window.onload = function(){setInterval(function(){window.location.href = '/%v';}, %v);};</script>",
|
htmlBody.WriteString(fmt.Sprintf("<script>window.onload = function(){setInterval(function(){window.location.href = '/%v';}, %v);};</script>",
|
||||||
queryParams,
|
queryParams,
|
||||||
refreshTimer))
|
refreshTimer))
|
||||||
|
@ -350,7 +350,7 @@ func serveHtmlHandler(paths []string, regexes *Regexes, fileCache *[]string) htt
|
||||||
|
|
||||||
sortOrder := getSortOrder(r)
|
sortOrder := getSortOrder(r)
|
||||||
|
|
||||||
refreshInterval := getRefreshInterval(r)
|
_, refreshInterval := getRefreshInterval(r, regexes)
|
||||||
|
|
||||||
if r.URL.Path == "/" {
|
if r.URL.Path == "/" {
|
||||||
var filePath string
|
var filePath string
|
||||||
|
@ -415,7 +415,7 @@ func serveHtmlHandler(paths []string, regexes *Regexes, fileCache *[]string) htt
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = serveHtml(w, r, filePath, dimensions, filters)
|
err = serveHtml(w, r, filePath, dimensions, filters, regexes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -436,6 +436,7 @@ func ServePage(args []string) error {
|
||||||
regexes := &Regexes{
|
regexes := &Regexes{
|
||||||
Filename: regexp.MustCompile(`(.+)([0-9]{3})(\..+)`),
|
Filename: regexp.MustCompile(`(.+)([0-9]{3})(\..+)`),
|
||||||
Alphanumeric: regexp.MustCompile(`^[a-zA-Z0-9]*$`),
|
Alphanumeric: regexp.MustCompile(`^[a-zA-Z0-9]*$`),
|
||||||
|
Units: regexp.MustCompile(`^[0-9]+(ns|us|µs|ms|s|m|h)$`),
|
||||||
}
|
}
|
||||||
|
|
||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
|
Loading…
Reference in New Issue