diff --git a/cmd/root.go b/cmd/root.go index 4c4143a..c99c8ab 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -11,7 +11,7 @@ import ( ) const ( - Version string = "0.52.2" + Version string = "0.53.0" ) var ( diff --git a/cmd/web.go b/cmd/web.go index b93051a..14fbdc6 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -12,7 +12,6 @@ import ( "errors" "fmt" "io" - "log" "math/rand" "net" "net/http" @@ -356,7 +355,7 @@ func (s *ServeStats) Export(path string) error { err = enc.Encode(&stats) if err != nil { - log.Fatal(err) + return err } return nil @@ -429,7 +428,30 @@ func notFound(w http.ResponseWriter, r *http.Request, filePath string) error { return nil } -func serverError() func(http.ResponseWriter, *http.Request, interface{}) { +func serverError(w http.ResponseWriter, r *http.Request) { + 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())) +} + +func serverErrorHandler() func(http.ResponseWriter, *http.Request, interface{}) { return func(w http.ResponseWriter, r *http.Request, i interface{}) { startTime := time.Now() @@ -620,7 +642,11 @@ func serveStats(args []string, stats *ServeStats) httprouter.Handle { first, last, err := stats.GetHistory() if err != nil { - log.Fatal(err) + fmt.Println(err) + + serverError(w, r) + + return } most := stats.GetMostServed() @@ -633,6 +659,8 @@ func serveStats(args []string, stats *ServeStats) httprouter.Handle { if err != nil { fmt.Println(err) + serverError(w, r) + return } @@ -723,6 +751,10 @@ func serveDebugJson(args []string, index *Index) httprouter.Handle { response, err := json.MarshalIndent(indexDump, "", " ") if err != nil { + fmt.Println(err) + + serverError(w, r) + return } @@ -747,6 +779,8 @@ func serveStaticFile(paths []string, stats *ServeStats) httprouter.Handle { if err != nil { fmt.Println(err) + serverError(w, r) + return } @@ -754,6 +788,8 @@ func serveStaticFile(paths []string, stats *ServeStats) httprouter.Handle { if err != nil { fmt.Println(err) + serverError(w, r) + return } @@ -767,6 +803,8 @@ func serveStaticFile(paths []string, stats *ServeStats) httprouter.Handle { if err != nil { fmt.Println(err) + serverError(w, r) + return } @@ -782,6 +820,8 @@ func serveStaticFile(paths []string, stats *ServeStats) httprouter.Handle { if err != nil { fmt.Println(err) + serverError(w, r) + return } @@ -811,6 +851,8 @@ func serveRoot(paths []string, Regexes *Regexes, index *Index) httprouter.Handle if err != nil { fmt.Println(err) + serverError(w, r) + return } @@ -832,6 +874,8 @@ func serveRoot(paths []string, Regexes *Regexes, index *Index) httprouter.Handle if err != nil { fmt.Println(err) + serverError(w, r) + return } } @@ -857,6 +901,8 @@ func serveRoot(paths []string, Regexes *Regexes, index *Index) httprouter.Handle case err != nil: fmt.Println(err) + serverError(w, r) + return } } @@ -891,6 +937,8 @@ func serveImage(paths []string, Regexes *Regexes, index *Index) httprouter.Handl if err != nil { fmt.Println(err) + serverError(w, r) + return } if !exists { @@ -903,6 +951,8 @@ func serveImage(paths []string, Regexes *Regexes, index *Index) httprouter.Handl if err != nil { fmt.Println(err) + serverError(w, r) + return } @@ -919,6 +969,8 @@ func serveImage(paths []string, Regexes *Regexes, index *Index) httprouter.Handl if err != nil { fmt.Println(err) + serverError(w, r) + return } } @@ -966,6 +1018,8 @@ func serveImage(paths []string, Regexes *Regexes, index *Index) httprouter.Handl if err != nil { fmt.Println(err) + serverError(w, r) + return } } @@ -1031,7 +1085,7 @@ func ServePage(args []string) error { mux := httprouter.New() - mux.PanicHandler = serverError() + mux.PanicHandler = serverErrorHandler() if cache { skipIndex := false