From 42734cda626dddd026ffea67258c6563ba4b1319 Mon Sep 17 00:00:00 2001 From: Seednode Date: Wed, 18 Jan 2023 12:11:23 -0600 Subject: [PATCH] Added /clear_cache endpoint to regenerate cache on demand --- README.md | 14 ++++++++++++-- cmd/version.go | 2 +- cmd/web.go | 31 +++++++++++++++++++++++-------- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 76219a1..06403bd 100644 --- a/README.md +++ b/README.md @@ -64,10 +64,20 @@ This can be used to generate a sort of slideshow of images. Supported units are `ns`, `us`/`µs`, `ms`, `s`, `m`, and `h`. +## Caching + +If the `-c|--cache` flag is passed, the contents of all specified paths will be cached on start. + +This will slightly increase the delay before the application begins responding to requests, but should significantly speed up subsequent requests. + +If any `include=`/`exclude=` filters are specified in a given request, the cache will be bypassed for that specific request. + +The cache can be regenerated any time by accessing the `/clear_cache` endpoint. + ## Usage output ``` Usage: - roulette [path2]... [flags] + roulette [path]... [flags] roulette [command] Available Commands: @@ -85,4 +95,4 @@ Flags: -v, --verbose log accessed files to stdout Use "roulette [command] --help" for more information about a command. -``` +``` \ No newline at end of file diff --git a/cmd/version.go b/cmd/version.go index 8133d9b..a724ae4 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -var Version = "0.30.0" +var Version = "0.30.1" func init() { rootCmd.AddCommand(versionCmd) diff --git a/cmd/web.go b/cmd/web.go index d06d924..318b775 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -59,14 +59,6 @@ func (f *Filters) GetExcludes() string { return strings.Join(f.Excludes, ",") } -func generateCache(args []string, fileCache *[]string) error { - filters := &Filters{} - fmt.Printf("%v | Preparing image cache...\n", time.Now().Format(LogDate)) - _, err := pickFile(args, filters, "", fileCache) - - return err -} - func notFound(w http.ResponseWriter, r *http.Request, filePath string) error { startTime := time.Now() @@ -335,6 +327,28 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string) err return nil } +func generateCache(args []string, fileCache *[]string) error { + filters := &Filters{} + + fileCache = &[]string{} + + fmt.Printf("%v | Preparing image cache...\n", time.Now().Format(LogDate)) + _, err := pickFile(args, filters, "", fileCache) + + return err +} + +func serveCacheClearHandler(args []string, fileCache *[]string) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if Cache { + err := generateCache(args, fileCache) + if err != nil { + fmt.Println(err) + } + } + } +} + func serveStaticFileHandler(paths []string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { err := serveStaticFile(w, r, paths) @@ -459,6 +473,7 @@ func ServePage(args []string) error { } http.Handle("/", serveHtmlHandler(paths, regexes, fileCache)) + http.Handle("/clear_cache", serveCacheClearHandler(args, fileCache)) http.Handle(Prefix+"/", http.StripPrefix(Prefix, serveStaticFileHandler(paths))) http.HandleFunc("/favicon.ico", doNothing)