diff --git a/cmd/root.go b/cmd/root.go
index 7f6e46c..ef218a8 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -12,7 +12,7 @@ import (
)
const (
- Version string = "0.54.0"
+ Version string = "0.55.0"
)
var (
diff --git a/cmd/web.go b/cmd/web.go
index b21a904..af0b3e0 100644
--- a/cmd/web.go
+++ b/cmd/web.go
@@ -274,68 +274,6 @@ func (s *ServeStats) ListImages() ([]byte, error) {
return r, nil
}
-func (s *ServeStats) GetHistory() (string, string, error) {
- stats := s.toExported()
-
- type History struct {
- name string
- time time.Time
- }
-
- firstServed := History{}
- lastServed := History{}
-
- for _, name := range stats.List {
- for _, accessTime := range stats.Times[name] {
- t, err := time.Parse(LogDate, accessTime)
- if err != nil {
- return "", "", err
- }
-
- if (firstServed.time == time.Time{}) {
- firstServed.name = name
- firstServed.time = t
- }
-
- if (lastServed.time == time.Time{}) {
- lastServed.name = name
- lastServed.time = t
- }
-
- switch {
- case t.Before(firstServed.time):
- firstServed.name = name
- firstServed.time = t
- case t.After(lastServed.time):
- lastServed.name = name
- lastServed.time = t
- }
- }
- }
-
- return firstServed.name, lastServed.name, nil
-}
-
-func (s *ServeStats) GetMostServed() string {
- stats := s.toExported()
-
- retVal := ""
-
- for _, v := range stats.List {
- if retVal == "" {
- retVal = v
-
- continue
- }
-
- if stats.Count[v] > stats.Count[retVal] {
- retVal = v
- }
- }
-
- return retVal
-}
-
func (s *ServeStats) Export(path string) error {
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
@@ -400,6 +338,32 @@ type timesServed struct {
Times []string
}
+func addFavicon() string {
+ var htmlBody strings.Builder
+
+ htmlBody.WriteString(``)
+ htmlBody.WriteString(``)
+ htmlBody.WriteString(``)
+ htmlBody.WriteString(``)
+ htmlBody.WriteString(``)
+ htmlBody.WriteString(``)
+ htmlBody.WriteString(``)
+
+ return htmlBody.String()
+}
+
+func newErrorPage(title, body string) string {
+ var htmlBody strings.Builder
+
+ htmlBody.WriteString(`
`)
+ htmlBody.WriteString(addFavicon())
+ htmlBody.WriteString(``)
+ htmlBody.WriteString(fmt.Sprintf("%s", title))
+ htmlBody.WriteString(fmt.Sprintf("%s", body))
+
+ return htmlBody.String()
+}
+
func notFound(w http.ResponseWriter, r *http.Request, filePath string) error {
startTime := time.Now()
@@ -414,13 +378,7 @@ func notFound(w http.ResponseWriter, r *http.Request, filePath string) error {
w.WriteHeader(http.StatusNotFound)
w.Header().Add("Content-Type", "text/html")
- var htmlBody strings.Builder
- htmlBody.WriteString(``)
- htmlBody.WriteString(``)
- htmlBody.WriteString(`Not Found`)
- htmlBody.WriteString(`404 page not found`)
-
- _, err := io.WriteString(w, gohtml.Format(htmlBody.String()))
+ _, err := io.WriteString(w, gohtml.Format(newErrorPage("Not Found", "404 Page not found")))
if err != nil {
return err
}
@@ -428,7 +386,7 @@ func notFound(w http.ResponseWriter, r *http.Request, filePath string) error {
return nil
}
-func serverError(w http.ResponseWriter, r *http.Request) {
+func serverError(w http.ResponseWriter, r *http.Request, i interface{}) {
startTime := time.Now()
if verbose {
@@ -442,38 +400,11 @@ func serverError(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
w.Header().Add("Content-Type", "text/html")
- var htmlBody strings.Builder
- htmlBody.WriteString(``)
- htmlBody.WriteString(``)
- htmlBody.WriteString(`Server Error`)
- htmlBody.WriteString(`500 Internal Server Error`)
-
- io.WriteString(w, gohtml.Format(htmlBody.String()))
+ io.WriteString(w, gohtml.Format(newErrorPage("Server Error", "500 Internal Server Error")))
}
func serverErrorHandler() func(http.ResponseWriter, *http.Request, interface{}) {
- return func(w http.ResponseWriter, r *http.Request, i interface{}) {
- startTime := time.Now()
-
- if verbose {
- fmt.Printf("%s | Invalid request for %s from %s\n",
- startTime.Format(LogDate),
- r.URL.Path,
- r.RemoteAddr,
- )
- }
-
- w.WriteHeader(http.StatusInternalServerError)
- w.Header().Add("Content-Type", "text/html")
-
- var htmlBody strings.Builder
- htmlBody.WriteString(``)
- htmlBody.WriteString(``)
- htmlBody.WriteString(`Server Error`)
- htmlBody.WriteString(`500 Internal Server Error`)
-
- io.WriteString(w, gohtml.Format(htmlBody.String()))
- }
+ return serverError
}
func RefreshInterval(r *http.Request, Regexes *Regexes) (int64, string) {
@@ -637,26 +568,11 @@ func serveStats(args []string, stats *ServeStats) httprouter.Handle {
startTime := time.Now()
- first, last, err := stats.GetHistory()
- if err != nil {
- fmt.Println(err)
-
- serverError(w, r)
-
- return
- }
-
- most := stats.GetMostServed()
-
- fmt.Printf("First served: %s\n", first)
- fmt.Printf("Last served: %s\n", last)
- fmt.Printf("Most served: %s\n", most)
-
response, err := stats.ListImages()
if err != nil {
fmt.Println(err)
- serverError(w, r)
+ serverError(w, r, nil)
return
}
@@ -694,13 +610,7 @@ func serveDebugHtml(args []string, index *Index) httprouter.Handle {
var htmlBody strings.Builder
htmlBody.WriteString(``)
- htmlBody.WriteString(``)
- htmlBody.WriteString(``)
- htmlBody.WriteString(``)
- htmlBody.WriteString(``)
- htmlBody.WriteString(``)
- htmlBody.WriteString(``)
- htmlBody.WriteString(``)
+ htmlBody.WriteString(addFavicon())
htmlBody.WriteString(``)
htmlBody.WriteString(`Index contains `)
htmlBody.WriteString(fileCount)
@@ -747,7 +657,7 @@ func serveDebugJson(args []string, index *Index) httprouter.Handle {
if err != nil {
fmt.Println(err)
- serverError(w, r)
+ serverError(w, r, nil)
return
}
@@ -773,7 +683,7 @@ func serveStaticFile(paths []string, stats *ServeStats) httprouter.Handle {
if err != nil {
fmt.Println(err)
- serverError(w, r)
+ serverError(w, r, nil)
return
}
@@ -782,7 +692,7 @@ func serveStaticFile(paths []string, stats *ServeStats) httprouter.Handle {
if err != nil {
fmt.Println(err)
- serverError(w, r)
+ serverError(w, r, nil)
return
}
@@ -797,7 +707,7 @@ func serveStaticFile(paths []string, stats *ServeStats) httprouter.Handle {
if err != nil {
fmt.Println(err)
- serverError(w, r)
+ serverError(w, r, nil)
return
}
@@ -814,7 +724,7 @@ func serveStaticFile(paths []string, stats *ServeStats) httprouter.Handle {
if err != nil {
fmt.Println(err)
- serverError(w, r)
+ serverError(w, r, nil)
return
}
@@ -845,7 +755,7 @@ func serveRoot(paths []string, Regexes *Regexes, index *Index) httprouter.Handle
if err != nil {
fmt.Println(err)
- serverError(w, r)
+ serverError(w, r, nil)
return
}
@@ -868,7 +778,7 @@ func serveRoot(paths []string, Regexes *Regexes, index *Index) httprouter.Handle
if err != nil {
fmt.Println(err)
- serverError(w, r)
+ serverError(w, r, nil)
return
}
@@ -895,7 +805,7 @@ func serveRoot(paths []string, Regexes *Regexes, index *Index) httprouter.Handle
case err != nil:
fmt.Println(err)
- serverError(w, r)
+ serverError(w, r, nil)
return
}
@@ -931,7 +841,7 @@ func serveImage(paths []string, Regexes *Regexes, index *Index) httprouter.Handl
if err != nil {
fmt.Println(err)
- serverError(w, r)
+ serverError(w, r, nil)
return
}
@@ -945,7 +855,7 @@ func serveImage(paths []string, Regexes *Regexes, index *Index) httprouter.Handl
if err != nil {
fmt.Println(err)
- serverError(w, r)
+ serverError(w, r, nil)
return
}
@@ -963,7 +873,7 @@ func serveImage(paths []string, Regexes *Regexes, index *Index) httprouter.Handl
if err != nil {
fmt.Println(err)
- serverError(w, r)
+ serverError(w, r, nil)
return
}
@@ -979,13 +889,7 @@ func serveImage(paths []string, Regexes *Regexes, index *Index) httprouter.Handl
var htmlBody strings.Builder
htmlBody.WriteString(``)
- htmlBody.WriteString(``)
- htmlBody.WriteString(``)
- htmlBody.WriteString(``)
- htmlBody.WriteString(``)
- htmlBody.WriteString(``)
- htmlBody.WriteString(``)
- htmlBody.WriteString(``)
+ htmlBody.WriteString(addFavicon())
htmlBody.WriteString(`