Added /clear_cache endpoint to regenerate cache on demand

This commit is contained in:
Seednode 2023-01-18 12:11:23 -06:00
parent b96ee8ac99
commit 42734cda62
3 changed files with 36 additions and 11 deletions

View File

@ -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`. 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 output
``` ```
Usage: Usage:
roulette <path> [path2]... [flags] roulette <path> [path]... [flags]
roulette [command] roulette [command]
Available Commands: Available Commands:
@ -85,4 +95,4 @@ Flags:
-v, --verbose log accessed files to stdout -v, --verbose log accessed files to stdout
Use "roulette [command] --help" for more information about a command. Use "roulette [command] --help" for more information about a command.
``` ```

View File

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

View File

@ -59,14 +59,6 @@ func (f *Filters) GetExcludes() string {
return strings.Join(f.Excludes, ",") 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 { func notFound(w http.ResponseWriter, r *http.Request, filePath string) error {
startTime := time.Now() startTime := time.Now()
@ -335,6 +327,28 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string) err
return nil 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 { func serveStaticFileHandler(paths []string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
err := serveStaticFile(w, r, paths) err := serveStaticFile(w, r, paths)
@ -459,6 +473,7 @@ func ServePage(args []string) error {
} }
http.Handle("/", serveHtmlHandler(paths, regexes, fileCache)) http.Handle("/", serveHtmlHandler(paths, regexes, fileCache))
http.Handle("/clear_cache", serveCacheClearHandler(args, fileCache))
http.Handle(Prefix+"/", http.StripPrefix(Prefix, serveStaticFileHandler(paths))) http.Handle(Prefix+"/", http.StripPrefix(Prefix, serveStaticFileHandler(paths)))
http.HandleFunc("/favicon.ico", doNothing) http.HandleFunc("/favicon.ico", doNothing)