Always append refresh query string, replaced string concatenation with strings.Builder in generateQueryParams()

This commit is contained in:
Seednode 2022-11-10 00:04:47 -06:00
parent 3e98201cdd
commit 0a251e8f5a
2 changed files with 18 additions and 30 deletions

View File

@ -10,7 +10,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var Version = "0.24.2" var Version = "0.24.3"
func init() { func init() {
rootCmd.AddCommand(versionCmd) rootCmd.AddCommand(versionCmd)

View File

@ -100,29 +100,22 @@ func splitQueryParams(query string) []string {
return params return params
} }
func generateQueryParams(filters *Filters, sortOrder, refreshInterval string) (string, error) { func generateQueryParams(filters *Filters, sortOrder, refreshInterval string) string {
refresh, err := strconv.Atoi(refreshInterval)
if err != nil {
return "", err
}
var hasParams bool var hasParams bool
var queryParams string var queryParams strings.Builder
if Filter || Sort || (refresh != 0) {
queryParams += "?" queryParams.WriteString("?")
}
if Filter { if Filter {
queryParams += "include=" queryParams.WriteString("include=")
if filters.HasIncludes() { if filters.HasIncludes() {
queryParams += filters.GetIncludes() queryParams.WriteString(filters.GetIncludes())
} }
queryParams += "&exclude=" queryParams.WriteString("&exclude=")
if filters.HasExcludes() { if filters.HasExcludes() {
queryParams += filters.GetExcludes() queryParams.WriteString(filters.GetExcludes())
} }
hasParams = true hasParams = true
@ -130,22 +123,20 @@ func generateQueryParams(filters *Filters, sortOrder, refreshInterval string) (s
if Sort { if Sort {
if hasParams { if hasParams {
queryParams += "&" queryParams.WriteString("&")
} }
queryParams += fmt.Sprintf("sort=%v", sortOrder) queryParams.WriteString(fmt.Sprintf("sort=%v", sortOrder))
hasParams = true hasParams = true
} }
if refresh != 0 { if hasParams {
if hasParams { queryParams.WriteString("&")
queryParams += "&"
}
queryParams += fmt.Sprintf("refresh=%v", refresh)
} }
queryParams.WriteString(fmt.Sprintf("refresh=%v", refreshInterval))
return queryParams, nil return queryParams.String()
} }
func stripQueryParams(u string) (string, error) { func stripQueryParams(u string) (string, error) {
@ -198,7 +189,7 @@ func serveHtml(w http.ResponseWriter, r *http.Request, filePath string, dimensio
refreshInterval = "0" refreshInterval = "0"
} }
queryParams, err := generateQueryParams(filters, r.URL.Query().Get("sort"), refreshInterval) queryParams := generateQueryParams(filters, r.URL.Query().Get("sort"), refreshInterval)
var htmlBody strings.Builder var htmlBody strings.Builder
htmlBody.WriteString(`<!DOCTYPE html><html lang="en"><head>`) htmlBody.WriteString(`<!DOCTYPE html><html lang="en"><head>`)
@ -227,7 +218,7 @@ func serveHtml(w http.ResponseWriter, r *http.Request, filePath string, dimensio
} }
htmlBody.WriteString(`</body></html>`) htmlBody.WriteString(`</body></html>`)
_, err = io.WriteString(w, gohtml.Format(htmlBody.String())) _, err := io.WriteString(w, gohtml.Format(htmlBody.String()))
if err != nil { if err != nil {
return err return err
} }
@ -333,10 +324,7 @@ func serveHtmlHandler(paths []string, re regexp.Regexp, fileCache *[]string) app
} }
} }
queryParams, err := generateQueryParams(&filters, sortOrder, refreshInterval) queryParams := generateQueryParams(&filters, sortOrder, refreshInterval)
if err != nil {
return err
}
newUrl := fmt.Sprintf("http://%v%v%v", newUrl := fmt.Sprintf("http://%v%v%v",
r.Host, r.Host,