Capitalize logging prefixes, limit values to math.MaxInt32

This commit is contained in:
Seednode 2023-09-26 05:29:55 -05:00
parent f7376e89b9
commit 81b2d4a7b4
5 changed files with 35 additions and 35 deletions

View File

@ -16,10 +16,10 @@ import (
) )
var ( var (
ErrInvalidFileCountRange = errors.New("maximum file count must be greater than or equal to minimum file count") ErrInvalidFileCountRange = errors.New("maximum file count limit must be greater than or equal to minimum file count limit")
ErrInvalidFileCountValue = errors.New("file count value must be an integer between 1 and 2147483647 inclusive") ErrInvalidFileCountValue = errors.New("file count limits must be positive integers no greater than 2147483647")
ErrInvalidPort = errors.New("listen port must be an integer between 1 and 65535 inclusive") ErrInvalidPort = errors.New("listen port must be an integer between 1 and 65535 inclusive")
ErrInvalidScanCount = errors.New("scan count must be a positive integer") ErrInvalidScanCount = errors.New("maximum scan count must be a positive integer no greater than 2147483647")
ErrNoMediaFound = errors.New("no supported media formats found which match all criteria") ErrNoMediaFound = errors.New("no supported media formats found which match all criteria")
) )
@ -39,7 +39,7 @@ func notFound(w http.ResponseWriter, r *http.Request, path string) error {
startTime := time.Now() startTime := time.Now()
if Verbose { if Verbose {
fmt.Printf("%s | Error: Unavailable file %s requested by %s\n", fmt.Printf("%s | ERROR: Unavailable file %s requested by %s\n",
startTime.Format(logDate), startTime.Format(logDate),
path, path,
r.RemoteAddr, r.RemoteAddr,
@ -61,17 +61,16 @@ func serverError(w http.ResponseWriter, r *http.Request, i interface{}) {
startTime := time.Now() startTime := time.Now()
if Verbose { if Verbose {
fmt.Printf("%s | Error: Invalid request for %s from %s\n", fmt.Printf("%s | ERROR: Invalid request for %s from %s\n",
startTime.Format(logDate), startTime.Format(logDate),
r.URL.Path, r.URL.Path,
r.RemoteAddr, r.RemoteAddr,
) )
} }
w.WriteHeader(http.StatusInternalServerError)
w.Header().Add("Content-Type", "text/html") w.Header().Add("Content-Type", "text/html")
io.WriteString(w, gohtml.Format(newErrorPage("Server Error", "500 Internal Server Error"))) io.WriteString(w, gohtml.Format(newErrorPage("Server Error", "An error has occurred. Please try again.")))
} }
func serverErrorHandler() func(http.ResponseWriter, *http.Request, interface{}) { func serverErrorHandler() func(http.ResponseWriter, *http.Request, interface{}) {

View File

@ -216,7 +216,7 @@ func pathIsValid(path string, paths []string) bool {
switch { switch {
case Verbose && !matchesPrefix: case Verbose && !matchesPrefix:
fmt.Printf("%s | Error: File outside specified path(s): %s\n", fmt.Printf("%s | ERROR: File outside specified path(s): %s\n",
time.Now().Format(logDate), time.Now().Format(logDate),
path, path,
) )
@ -432,7 +432,7 @@ Poll:
} }
if Verbose { if Verbose {
fmt.Printf("%s | Index: %d/%d files across %d/%d directories in %s\n", fmt.Printf("%s | INDEX: %d/%d files across %d/%d directories in %s\n",
time.Now().Format(logDate), time.Now().Format(logDate),
stats.filesMatched, stats.filesMatched,
stats.filesMatched+stats.filesSkipped, stats.filesMatched+stats.filesSkipped,

View File

@ -178,7 +178,7 @@ func serveIndexJson(args []string, index *fileCache, errorChannel chan<- error)
w.Write(response) w.Write(response)
if Verbose { if Verbose {
fmt.Printf("%s | Serve: JSON index page (%s) to %s in %s\n", fmt.Printf("%s | SERVE: JSON index page (%s) to %s in %s\n",
startTime.Format(logDate), startTime.Format(logDate),
humanReadableSize(len(response)), humanReadableSize(len(response)),
realIP(r), realIP(r),
@ -199,7 +199,7 @@ func serveAvailableExtensions() httprouter.Handle {
w.Write(response) w.Write(response)
if Verbose { if Verbose {
fmt.Printf("%s | Serve: Available extension list (%s) to %s in %s\n", fmt.Printf("%s | SERVE: Available extension list (%s) to %s in %s\n",
startTime.Format(logDate), startTime.Format(logDate),
humanReadableSize(len(response)), humanReadableSize(len(response)),
realIP(r), realIP(r),
@ -220,7 +220,7 @@ func serveEnabledExtensions(formats *types.Types) httprouter.Handle {
w.Write(response) w.Write(response)
if Verbose { if Verbose {
fmt.Printf("%s | Serve: Registered extension list (%s) to %s in %s\n", fmt.Printf("%s | SERVE: Registered extension list (%s) to %s in %s\n",
startTime.Format(logDate), startTime.Format(logDate),
humanReadableSize(len(response)), humanReadableSize(len(response)),
realIP(r), realIP(r),
@ -241,7 +241,7 @@ func serveAvailableMimeTypes() httprouter.Handle {
w.Write(response) w.Write(response)
if Verbose { if Verbose {
fmt.Printf("%s | Serve: Available MIME type list (%s) to %s in %s\n", fmt.Printf("%s | SERVE: Available MIME type list (%s) to %s in %s\n",
startTime.Format(logDate), startTime.Format(logDate),
humanReadableSize(len(response)), humanReadableSize(len(response)),
realIP(r), realIP(r),
@ -262,7 +262,7 @@ func serveEnabledMimeTypes(formats *types.Types) httprouter.Handle {
w.Write(response) w.Write(response)
if Verbose { if Verbose {
fmt.Printf("%s | Serve: Registered MIME type list (%s) to %s in %s\n", fmt.Printf("%s | SERVE: Registered MIME type list (%s) to %s in %s\n",
startTime.Format(logDate), startTime.Format(logDate),
humanReadableSize(len(response)), humanReadableSize(len(response)),
realIP(r), realIP(r),

View File

@ -6,12 +6,13 @@ package cmd
import ( import (
"log" "log"
"math"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
const ( const (
ReleaseVersion string = "0.93.2" ReleaseVersion string = "0.94.0"
) )
var ( var (
@ -51,23 +52,14 @@ var (
Short: "Serves random media from the specified directories.", Short: "Serves random media from the specified directories.",
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error { PreRunE: func(cmd *cobra.Command, args []string) error {
if MaxDirScans < 1 || MaxFileScans < 1 { switch {
case MaxDirScans < 1 || MaxFileScans < 1 || MaxDirScans > math.MaxInt32 || MaxFileScans > math.MaxInt32:
return ErrInvalidScanCount return ErrInvalidScanCount
} case MaxFileCount < 1 || MinFileCount < 1 || MaxFileCount > math.MaxInt32 || MinFileCount > math.MaxInt32:
if MaxFileCount > 1<<31-1 || MaxFileCount < 1 {
return ErrInvalidFileCountValue return ErrInvalidFileCountValue
} case MinFileCount > MaxFileCount:
if MinFileCount > 1<<31-1 || MinFileCount < 1 {
return ErrInvalidFileCountValue
}
if MinFileCount > MaxFileCount {
return ErrInvalidFileCountRange return ErrInvalidFileCountRange
} case Port < 1 || Port > 65535:
if Port < 1 || Port > 65535 {
return ErrInvalidPort return ErrInvalidPort
} }
@ -108,7 +100,7 @@ func init() {
rootCmd.Flags().BoolVarP(&Info, "info", "i", false, "expose informational endpoints") rootCmd.Flags().BoolVarP(&Info, "info", "i", false, "expose informational endpoints")
rootCmd.Flags().IntVar(&MaxDirScans, "max-directory-scans", 32, "number of directories to scan at once") rootCmd.Flags().IntVar(&MaxDirScans, "max-directory-scans", 32, "number of directories to scan at once")
rootCmd.Flags().IntVar(&MaxFileScans, "max-file-scans", 256, "number of files to scan at once") rootCmd.Flags().IntVar(&MaxFileScans, "max-file-scans", 256, "number of files to scan at once")
rootCmd.Flags().IntVar(&MaxFileCount, "max-file-count", 1<<31-1, "skip directories with file counts above this value") rootCmd.Flags().IntVar(&MaxFileCount, "max-file-count", math.MaxInt32, "skip directories with file counts above this value")
rootCmd.Flags().IntVar(&MinFileCount, "min-file-count", 1, "skip directories with file counts below this value") rootCmd.Flags().IntVar(&MinFileCount, "min-file-count", 1, "skip directories with file counts below this value")
rootCmd.Flags().IntVar(&PageLength, "page-length", 0, "pagination length for info pages") rootCmd.Flags().IntVar(&PageLength, "page-length", 0, "pagination length for info pages")
rootCmd.Flags().IntVarP(&Port, "port", "p", 8080, "port to listen on") rootCmd.Flags().IntVarP(&Port, "port", "p", 8080, "port to listen on")

View File

@ -140,7 +140,7 @@ func serveStaticFile(paths []string, cache *fileCache, errorChannel chan<- error
} }
if Verbose { if Verbose {
fmt.Printf("%s | Serve: %s (%s) to %s in %s%s\n", fmt.Printf("%s | SERVE: %s (%s) to %s in %s%s\n",
startTime.Format(logDate), startTime.Format(logDate),
filePath, filePath,
humanReadableSize(written), humanReadableSize(written),
@ -336,7 +336,7 @@ func serveMedia(paths []string, regexes *regexes, cache *fileCache, formats *typ
if format.Type() != "embed" { if format.Type() != "embed" {
if Verbose { if Verbose {
fmt.Printf("%s | Serve: %s (%s) to %s in %s\n", fmt.Printf("%s | SERVE: %s (%s) to %s in %s\n",
startTime.Format(logDate), startTime.Format(logDate),
path, path,
humanReadableSize(written), humanReadableSize(written),
@ -452,8 +452,17 @@ func ServePage(args []string) error {
mux := httprouter.New() mux := httprouter.New()
listenHost := net.JoinHostPort(Bind, strconv.Itoa(Port))
if Verbose {
fmt.Printf("%s | SERVE: Listening on %s...\n",
time.Now().Format(logDate),
listenHost,
)
}
srv := &http.Server{ srv := &http.Server{
Addr: net.JoinHostPort(Bind, strconv.Itoa(Port)), Addr: listenHost,
Handler: mux, Handler: mux,
IdleTimeout: 10 * time.Minute, IdleTimeout: 10 * time.Minute,
ReadTimeout: 5 * time.Second, ReadTimeout: 5 * time.Second,
@ -507,10 +516,10 @@ func ServePage(args []string) error {
go func() { go func() {
for err := range errorChannel { for err := range errorChannel {
fmt.Printf("%s | Error: %v\n", time.Now().Format(logDate), err) fmt.Printf("%s | ERROR: %v\n", time.Now().Format(logDate), err)
if ExitOnError { if ExitOnError {
fmt.Printf("%s | Error: Shutting down...\n", time.Now().Format(logDate)) fmt.Printf("%s | ERROR: Shutting down...\n", time.Now().Format(logDate))
srv.Shutdown(context.Background()) srv.Shutdown(context.Background())
} }