Added support for persistent index file
This commit is contained in:
parent
2f92089191
commit
69c4ccfc97
|
@ -92,13 +92,14 @@ Available Commands:
|
|||
version Print version
|
||||
|
||||
Flags:
|
||||
-c, --cache only scan directories once, at startup (or when filters are applied)
|
||||
-d, --debug store list of files served and number of times they were served
|
||||
-f, --filter enable filtering via query parameters
|
||||
-c, --cache generate directory cache at startup
|
||||
-d, --debug expose stats endpoint
|
||||
-f, --filter enable filtering
|
||||
-h, --help help for roulette
|
||||
-i, --index string path to persistent index file
|
||||
-p, --port uint16 port to listen on (default 8080)
|
||||
-r, --recursive recurse into subdirectories
|
||||
-s, --sort enable sorting via query parameters
|
||||
-s, --sort enable sorting
|
||||
-v, --verbose log accessed files to stdout
|
||||
|
||||
Use "roulette [command] --help" for more information about a command.
|
||||
|
|
|
@ -15,6 +15,7 @@ var (
|
|||
cache bool
|
||||
debug bool
|
||||
filtering bool
|
||||
indexFile string
|
||||
port uint16
|
||||
recursive bool
|
||||
sorting bool
|
||||
|
@ -47,6 +48,7 @@ func init() {
|
|||
rootCmd.Flags().BoolVarP(&cache, "cache", "c", false, "generate directory cache at startup")
|
||||
rootCmd.Flags().BoolVarP(&debug, "debug", "d", false, "expose stats endpoint")
|
||||
rootCmd.Flags().BoolVarP(&filtering, "filter", "f", false, "enable filtering")
|
||||
rootCmd.Flags().StringVarP(&indexFile, "index", "i", "", "path to persistent index file")
|
||||
rootCmd.Flags().Uint16VarP(&port, "port", "p", 8080, "port to listen on")
|
||||
rootCmd.Flags().BoolVarP(&recursive, "recursive", "r", false, "recurse into subdirectories")
|
||||
rootCmd.Flags().BoolVarP(&sorting, "sort", "s", false, "enable sorting")
|
||||
|
|
|
@ -10,7 +10,7 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var Version = "0.34.2"
|
||||
var Version = "0.35.0"
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(versionCmd)
|
||||
|
|
62
cmd/web.go
62
cmd/web.go
|
@ -5,7 +5,9 @@ Copyright © 2023 Seednode <seednode@seedno.de>
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/gob"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
|
@ -25,6 +27,10 @@ import (
|
|||
"github.com/yosssi/gohtml"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrIndexNotExist = errors.New(`specified index does not exist`)
|
||||
)
|
||||
|
||||
const (
|
||||
LogDate string = `2006-01-02T15:04:05.000-07:00`
|
||||
Prefix string = `/src`
|
||||
|
@ -87,6 +93,10 @@ func (i *Index) generateCache(args []string) {
|
|||
i.mutex.Unlock()
|
||||
|
||||
fileList(args, &Filters{}, "", i)
|
||||
|
||||
if indexFile != "" {
|
||||
i.Export(indexFile)
|
||||
}
|
||||
}
|
||||
|
||||
func (i *Index) IsEmpty() bool {
|
||||
|
@ -97,6 +107,39 @@ func (i *Index) IsEmpty() bool {
|
|||
return length == 0
|
||||
}
|
||||
|
||||
func (i *Index) Export(path string) error {
|
||||
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
enc := gob.NewEncoder(file)
|
||||
|
||||
enc.Encode(&i.list)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *Index) Import(path string) error {
|
||||
file, err := os.OpenFile(path, os.O_RDONLY, 0600)
|
||||
if os.IsNotExist(err) {
|
||||
return ErrIndexNotExist
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
dec := gob.NewDecoder(file)
|
||||
|
||||
err = dec.Decode(&i.list)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type ServeStats struct {
|
||||
mutex sync.RWMutex
|
||||
list []string
|
||||
|
@ -577,7 +620,9 @@ func ServePage(args []string) error {
|
|||
units: regexp.MustCompile(`^[0-9]+(ns|us|µs|ms|s|m|h)$`),
|
||||
}
|
||||
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
//NewRand(NewSource(time.Now().UnixNano()))
|
||||
|
||||
rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
|
||||
index := &Index{
|
||||
mutex: sync.RWMutex{},
|
||||
|
@ -585,7 +630,22 @@ func ServePage(args []string) error {
|
|||
}
|
||||
|
||||
if cache {
|
||||
skipIndex := false
|
||||
|
||||
if indexFile != "" {
|
||||
err = index.Import(indexFile)
|
||||
switch err {
|
||||
case ErrIndexNotExist:
|
||||
case nil:
|
||||
skipIndex = true
|
||||
default:
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if !skipIndex {
|
||||
index.generateCache(args)
|
||||
}
|
||||
|
||||
http.Handle("/_/clear_cache", serveCacheClearHandler(args, index))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue