From 70275a734f9d51bd259df90c79ae060d912a3516 Mon Sep 17 00:00:00 2001 From: Seednode Date: Tue, 30 Jan 2024 14:38:05 -0600 Subject: [PATCH] Rework log messages for index rebuilds (scheduled and requested) --- cmd/index.go | 109 +++++++++++++++++++++++++++++++++++++++++---------- cmd/info.go | 71 --------------------------------- cmd/root.go | 2 +- 3 files changed, 89 insertions(+), 93 deletions(-) diff --git a/cmd/index.go b/cmd/index.go index bb325f9..151c7c6 100644 --- a/cmd/index.go +++ b/cmd/index.go @@ -6,11 +6,16 @@ package cmd import ( "encoding/gob" + "encoding/json" "fmt" + "net/http" "os" + "sort" + "strings" "sync" "time" + "github.com/julienschmidt/httprouter" "github.com/klauspost/compress/zstd" "seedno.de/seednode/roulette/types" ) @@ -186,6 +191,85 @@ func (index *fileIndex) Import(path string, errorChannel chan<- error) { } } +func rebuildIndex(args []string, index *fileIndex, formats types.Types, encoder *zstd.Encoder, errorChannel chan<- error) { + index.clear() + + fileList(args, &filters{}, "", index, formats, encoder, errorChannel) +} + +func importIndex(args []string, index *fileIndex, formats types.Types, encoder *zstd.Encoder, errorChannel chan<- error) { + if IndexFile != "" { + index.Import(IndexFile, errorChannel) + } + + fileList(args, &filters{}, "", index, formats, encoder, errorChannel) +} + +func serveIndex(args []string, index *fileIndex, errorChannel chan<- error) httprouter.Handle { + return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + startTime := time.Now() + + w.Header().Add("Content-Security-Policy", "default-src 'self';") + + w.Header().Set("Content-Type", "application/json;charset=UTF-8") + + indexDump := index.List() + + sort.SliceStable(indexDump, func(p, q int) bool { + return strings.ToLower(indexDump[p]) < strings.ToLower(indexDump[q]) + }) + + response, err := json.MarshalIndent(indexDump, "", " ") + if err != nil { + errorChannel <- err + + serverError(w, r, nil) + + return + } + + response = append(response, []byte("\n")...) + + written, err := w.Write(response) + if err != nil { + errorChannel <- err + } + + if Verbose { + fmt.Printf("%s | SERVE: JSON index page (%s) to %s in %s\n", + startTime.Format(logDate), + humanReadableSize(written), + realIP(r), + time.Since(startTime).Round(time.Microsecond), + ) + } + } +} + +func serveIndexRebuild(args []string, index *fileIndex, formats types.Types, encoder *zstd.Encoder, errorChannel chan<- error) httprouter.Handle { + return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + if Verbose { + fmt.Printf("%s | SERVE: Index rebuild requested by %s\n", + time.Now().Format(logDate), + realIP(r), + ) + } + + w.Header().Add("Content-Security-Policy", "default-src 'self';") + + w.Header().Set("Content-Type", "text/plain;charset=UTF-8") + + rebuildIndex(args, index, formats, encoder, errorChannel) + + _, err := w.Write([]byte("Ok\n")) + if err != nil { + errorChannel <- err + + return + } + } +} + func registerIndexInterval(args []string, index *fileIndex, formats types.Types, encoder *zstd.Encoder, quit <-chan struct{}, errorChannel chan<- error) { interval, err := time.ParseDuration(IndexInterval) if err != nil { @@ -200,16 +284,13 @@ func registerIndexInterval(args []string, index *fileIndex, formats types.Types, for { select { case <-ticker.C: - startTime := time.Now() - - rebuildIndex(args, index, formats, encoder, errorChannel) - if Verbose { - fmt.Printf("%s | INDEX: Automatic rebuild took %s\n", - startTime.Format(logDate), - time.Since(startTime).Round(time.Microsecond), + fmt.Printf("%s | INDEX: Started scheduled index rebuild\n", + time.Now().Format(logDate), ) } + + rebuildIndex(args, index, formats, encoder, errorChannel) case <-quit: ticker.Stop() @@ -218,17 +299,3 @@ func registerIndexInterval(args []string, index *fileIndex, formats types.Types, } }() } - -func rebuildIndex(args []string, index *fileIndex, formats types.Types, encoder *zstd.Encoder, errorChannel chan<- error) { - index.clear() - - fileList(args, &filters{}, "", index, formats, encoder, errorChannel) -} - -func importIndex(args []string, index *fileIndex, formats types.Types, encoder *zstd.Encoder, errorChannel chan<- error) { - if IndexFile != "" { - index.Import(IndexFile, errorChannel) - } - - fileList(args, &filters{}, "", index, formats, encoder, errorChannel) -} diff --git a/cmd/info.go b/cmd/info.go index c59715b..226d503 100644 --- a/cmd/info.go +++ b/cmd/info.go @@ -5,11 +5,8 @@ Copyright © 2024 Seednode package cmd import ( - "encoding/json" "fmt" "net/http" - "sort" - "strings" "time" "github.com/julienschmidt/httprouter" @@ -49,74 +46,6 @@ func serveExtensions(formats types.Types, available bool, errorChannel chan<- er } } -func serveIndex(args []string, index *fileIndex, errorChannel chan<- error) httprouter.Handle { - return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { - startTime := time.Now() - - w.Header().Add("Content-Security-Policy", "default-src 'self';") - - w.Header().Set("Content-Type", "application/json;charset=UTF-8") - - indexDump := index.List() - - sort.SliceStable(indexDump, func(p, q int) bool { - return strings.ToLower(indexDump[p]) < strings.ToLower(indexDump[q]) - }) - - response, err := json.MarshalIndent(indexDump, "", " ") - if err != nil { - errorChannel <- err - - serverError(w, r, nil) - - return - } - - response = append(response, []byte("\n")...) - - written, err := w.Write(response) - if err != nil { - errorChannel <- err - } - - if Verbose { - fmt.Printf("%s | SERVE: JSON index page (%s) to %s in %s\n", - startTime.Format(logDate), - humanReadableSize(written), - realIP(r), - time.Since(startTime).Round(time.Microsecond), - ) - } - } -} - -func serveIndexRebuild(args []string, index *fileIndex, formats types.Types, encoder *zstd.Encoder, errorChannel chan<- error) httprouter.Handle { - return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { - startTime := time.Now() - - w.Header().Add("Content-Security-Policy", "default-src 'self';") - - w.Header().Set("Content-Type", "text/plain;charset=UTF-8") - - rebuildIndex(args, index, formats, encoder, errorChannel) - - _, err := w.Write([]byte("Ok\n")) - if err != nil { - errorChannel <- err - - return - } - - if Verbose { - fmt.Printf("%s | SERVE: Index rebuild requested by %s took %s\n", - startTime.Format(logDate), - realIP(r), - time.Since(startTime).Round(time.Microsecond), - ) - } - } -} - func serveMediaTypes(formats types.Types, available bool, errorChannel chan<- error) httprouter.Handle { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { startTime := time.Now() diff --git a/cmd/root.go b/cmd/root.go index 41450da..50f1a99 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -17,7 +17,7 @@ import ( const ( AllowedCharacters string = `^[A-z0-9.\-_]+$` - ReleaseVersion string = "8.3.0" + ReleaseVersion string = "8.3.1" ) var (