diff --git a/cmd/favicons.go b/cmd/favicons.go index 5350529..9e9d807 100644 --- a/cmd/favicons.go +++ b/cmd/favicons.go @@ -27,7 +27,7 @@ const ( ` ) -func serveFavicons() httprouter.Handle { +func serveFavicons(errorChannel chan<- error) httprouter.Handle { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { fname := strings.TrimPrefix(r.URL.Path, "/") @@ -36,8 +36,18 @@ func serveFavicons() httprouter.Handle { return } - w.Header().Write(bytes.NewBufferString("Content-Length: " + strconv.Itoa(len(data)))) + err = w.Header().Write(bytes.NewBufferString("Content-Length: " + strconv.Itoa(len(data)))) + if err != nil { + errorChannel <- err - w.Write(data) + return + } + + _, err = w.Write(data) + if err != nil { + errorChannel <- err + + return + } } } diff --git a/cmd/index.go b/cmd/index.go index 905e33d..07928f4 100644 --- a/cmd/index.go +++ b/cmd/index.go @@ -262,13 +262,28 @@ func (index *fileIndex) Import(path string) error { func serveIndexRebuild(args []string, index *fileIndex, formats types.Types, errorChannel chan<- error) httprouter.Handle { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + startTime := time.Now() + index.clear() fileList(args, &filters{}, "", index, formats, errorChannel) w.Header().Set("Content-Type", "text/plain") - w.Write([]byte("Ok\n")) + _, 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), + ) + } } } diff --git a/cmd/info.go b/cmd/info.go index ff12dcd..f42c9dc 100644 --- a/cmd/info.go +++ b/cmd/info.go @@ -145,7 +145,7 @@ func serveIndexHtml(args []string, index *fileIndex, shouldPaginate bool) httpro htmlBody.WriteString(``) - length, err := io.WriteString(w, gohtml.Format(htmlBody.String())) + written, err := io.WriteString(w, gohtml.Format(htmlBody.String())) if err != nil { return } @@ -153,7 +153,7 @@ func serveIndexHtml(args []string, index *fileIndex, shouldPaginate bool) httpro if Verbose { fmt.Printf("%s | SERVE: HTML index page (%s) to %s in %s\n", startTime.Format(logDate), - humanReadableSize(length), + humanReadableSize(written), realIP(r), time.Since(startTime).Round(time.Microsecond), ) diff --git a/cmd/root.go b/cmd/root.go index 370da69..f61cb92 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -17,7 +17,7 @@ import ( const ( AllowedCharacters string = `^[A-z0-9.\-_]+$` - ReleaseVersion string = "4.0.3" + ReleaseVersion string = "4.0.4" ) var ( diff --git a/cmd/web.go b/cmd/web.go index 008944a..72ffd08 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -253,6 +253,8 @@ func serveRoot(paths []string, regexes *regexes, index *fileIndex, formats types func serveMedia(paths []string, regexes *regexes, index *fileIndex, formats types.Types, errorChannel chan<- error) httprouter.Handle { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + startTime := time.Now() + filters := &filters{ included: splitQueryParams(r.URL.Query().Get("include"), regexes), excluded: splitQueryParams(r.URL.Query().Get("exclude"), regexes), @@ -384,8 +386,6 @@ func serveMedia(paths []string, regexes *regexes, index *fileIndex, formats type htmlBody.WriteString(``) - startTime := time.Now() - formattedPage := gohtml.Format(htmlBody.String()) written, err := io.WriteString(w, formattedPage) @@ -409,19 +409,45 @@ func serveMedia(paths []string, regexes *regexes, index *fileIndex, formats type } if Russian { - kill(path, index) + err := kill(path, index) + if err != nil { + errorChannel <- err + + return + } } } } } -func serveVersion() httprouter.Handle { +func serveVersion(errorChannel chan<- error) httprouter.Handle { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + startTime := time.Now() + data := []byte(fmt.Sprintf("roulette v%s\n", ReleaseVersion)) - w.Header().Write(bytes.NewBufferString("Content-Length: " + strconv.Itoa(len(data)))) + err := w.Header().Write(bytes.NewBufferString("Content-Length: " + strconv.Itoa(len(data)))) + if err != nil { + errorChannel <- err - w.Write(data) + return + } + + written, err := w.Write(data) + if err != nil { + errorChannel <- err + + return + } + + if Verbose { + fmt.Printf("%s | SERVE: Version page (%s) to %s in %s\n", + startTime.Format(logDate), + humanReadableSize(written), + realIP(r), + time.Since(startTime).Round(time.Microsecond), + ) + } } } @@ -578,15 +604,15 @@ func ServePage(args []string) error { registerHandler(mux, "/", redirectRoot()) } - registerHandler(mux, Prefix+"/favicons/*favicon", serveFavicons()) + registerHandler(mux, Prefix+"/favicons/*favicon", serveFavicons(errorChannel)) - registerHandler(mux, Prefix+"/favicon.ico", serveFavicons()) + registerHandler(mux, Prefix+"/favicon.ico", serveFavicons(errorChannel)) registerHandler(mux, Prefix+mediaPrefix+"/*media", serveMedia(paths, regexes, index, formats, errorChannel)) registerHandler(mux, Prefix+sourcePrefix+"/*static", serveStaticFile(paths, index, errorChannel)) - registerHandler(mux, Prefix+"/version", serveVersion()) + registerHandler(mux, Prefix+"/version", serveVersion(errorChannel)) if Index { registerHandler(mux, Prefix+AdminPrefix+"/index/rebuild", serveIndexRebuild(args, index, formats, errorChannel))