2023-09-11 17:09:08 +00:00
|
|
|
/*
|
|
|
|
Copyright © 2023 Seednode <seednode@seedno.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2023-09-11 17:59:40 +00:00
|
|
|
"errors"
|
2023-09-11 17:09:08 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/yosssi/gohtml"
|
|
|
|
)
|
|
|
|
|
2023-09-11 17:59:40 +00:00
|
|
|
var (
|
2023-12-31 22:04:54 +00:00
|
|
|
ErrInvalidAdminPrefix = errors.New("admin path must match the pattern " + AllowedCharacters)
|
2024-01-09 19:39:08 +00:00
|
|
|
ErrInvalidCompression = errors.New("supported compression formats: none, zlib, zstd")
|
2024-01-01 17:51:28 +00:00
|
|
|
ErrInvalidConcurrency = errors.New("concurrency limit must be a positive integer")
|
2023-09-26 10:29:55 +00:00
|
|
|
ErrInvalidFileCountRange = errors.New("maximum file count limit must be greater than or equal to minimum file count limit")
|
2023-10-03 20:00:32 +00:00
|
|
|
ErrInvalidFileCountValue = errors.New("file count limits must be non-negative integers no greater than 2147483647")
|
2023-12-31 22:04:54 +00:00
|
|
|
ErrInvalidIgnoreFile = errors.New("ignore filename must match the pattern " + AllowedCharacters)
|
2023-09-26 09:03:52 +00:00
|
|
|
ErrInvalidPort = errors.New("listen port must be an integer between 1 and 65535 inclusive")
|
|
|
|
ErrNoMediaFound = errors.New("no supported media formats found which match all criteria")
|
2023-09-11 17:59:40 +00:00
|
|
|
)
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
func notFound(w http.ResponseWriter, r *http.Request, path string) error {
|
2023-09-11 17:09:08 +00:00
|
|
|
startTime := time.Now()
|
|
|
|
|
2024-01-06 00:50:45 +00:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
w.Header().Add("Content-Type", "text/html")
|
|
|
|
|
|
|
|
_, err := io.WriteString(w, gohtml.Format(newPage("Not Found", "404 Page not found")))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-12 18:06:45 +00:00
|
|
|
if Verbose {
|
2023-09-26 10:29:55 +00:00
|
|
|
fmt.Printf("%s | ERROR: Unavailable file %s requested by %s\n",
|
2023-09-13 14:26:15 +00:00
|
|
|
startTime.Format(logDate),
|
|
|
|
path,
|
2023-09-11 17:09:08 +00:00
|
|
|
r.RemoteAddr,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func serverError(w http.ResponseWriter, r *http.Request, i interface{}) {
|
|
|
|
startTime := time.Now()
|
|
|
|
|
2024-01-06 00:50:45 +00:00
|
|
|
w.Header().Add("Content-Type", "text/html")
|
|
|
|
|
|
|
|
io.WriteString(w, gohtml.Format(newPage("Server Error", "An error has occurred. Please try again.")))
|
|
|
|
|
2023-09-12 18:06:45 +00:00
|
|
|
if Verbose {
|
2023-09-26 10:29:55 +00:00
|
|
|
fmt.Printf("%s | ERROR: Invalid request for %s from %s\n",
|
2023-09-13 14:26:15 +00:00
|
|
|
startTime.Format(logDate),
|
2023-09-11 17:09:08 +00:00
|
|
|
r.URL.Path,
|
|
|
|
r.RemoteAddr,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func serverErrorHandler() func(http.ResponseWriter, *http.Request, interface{}) {
|
|
|
|
return serverError
|
|
|
|
}
|