From ed68c6fc044a31d9589a6d3cf8d885f382ca9c0c Mon Sep 17 00:00:00 2001 From: Azareal Date: Tue, 23 Jul 2019 20:34:44 +1000 Subject: [PATCH] Add a simple letter favicon. Disable Gzip for favicons, it doesn't make them smaller anyway. Eliminate an allocation for static files. Fixes #59 --- common/files.go | 7 ++++--- gen_router.go | 15 +++++++++++---- public/favicon.ico | Bin 0 -> 1086 bytes router_gen/main.go | 15 +++++++++++---- routes/misc.go | 4 ++-- 5 files changed, 28 insertions(+), 13 deletions(-) create mode 100644 public/favicon.ico diff --git a/common/files.go b/common/files.go index 57372d8b..c7893215 100644 --- a/common/files.go +++ b/common/files.go @@ -32,6 +32,7 @@ type SFile struct { Pos int64 Length int64 GzipLength int64 + StrGzipLength string Mimetype string Info os.FileInfo FormattedModTime string @@ -239,7 +240,7 @@ func (list SFileList) JSTmplInit() error { hasher.Write(data) checksum := hex.EncodeToString(hasher.Sum(nil)) - list.Set("/static/"+path, SFile{data, gzipData, checksum,path + "?h=" + checksum, 0, int64(len(data)), int64(len(gzipData)), mime.TypeByExtension(ext), f, f.ModTime().UTC().Format(http.TimeFormat)}) + list.Set("/static/"+path, SFile{data, gzipData, checksum,path + "?h=" + checksum, 0, int64(len(data)), int64(len(gzipData)),strconv.Itoa(len(gzipData)), mime.TypeByExtension(ext), f, f.ModTime().UTC().Format(http.TimeFormat)}) DebugLogf("Added the '%s' static file.", path) return nil @@ -284,7 +285,7 @@ func (list SFileList) Init() error { } } - list.Set("/static/"+path, SFile{data, gzipData, checksum,path + "?h=" + checksum, 0, int64(len(data)), int64(len(gzipData)), mimetype, f, f.ModTime().UTC().Format(http.TimeFormat)}) + list.Set("/static/"+path, SFile{data, gzipData, checksum,path + "?h=" + checksum, 0, int64(len(data)), int64(len(gzipData)),strconv.Itoa(len(gzipData)), mimetype, f, f.ModTime().UTC().Format(http.TimeFormat)}) DebugLogf("Added the '%s' static file.", path) return nil @@ -317,7 +318,7 @@ func (list SFileList) Add(path string, prefix string) error { hasher.Write(data) checksum := hex.EncodeToString(hasher.Sum(nil)) - list.Set("/static"+path, SFile{data, gzipData, checksum,path + "?h=" + checksum, 0, int64(len(data)), int64(len(gzipData)), mime.TypeByExtension(ext), f, f.ModTime().UTC().Format(http.TimeFormat)}) + list.Set("/static"+path, SFile{data, gzipData, checksum,path + "?h=" + checksum, 0, int64(len(data)), int64(len(gzipData)),strconv.Itoa(len(gzipData)), mime.TypeByExtension(ext), f, f.ModTime().UTC().Format(http.TimeFormat)}) DebugLogf("Added the '%s' static file", path) return nil diff --git a/gen_router.go b/gen_router.go index 16b484f8..8293b34e 100644 --- a/gen_router.go +++ b/gen_router.go @@ -2290,8 +2290,9 @@ func (r *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, user c gzw, ok := w.(c.GzipResponseWriter) if ok { w = gzw.ResponseWriter - w.Header().Del("Content-Type") - w.Header().Del("Content-Encoding") + h := w.Header() + h.Del("Content-Type") + h.Del("Content-Encoding") } counters.RouteViewCounter.Bump(142) req.URL.Path += extraData @@ -2306,8 +2307,14 @@ func (r *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, user c counters.RouteViewCounter.Bump(144) return routes.RobotsTxt(w,req) case "favicon.ico": - req.URL.Path = "/static"+req.URL.Path+extraData - //log.Print("req.URL.Path: ",req.URL.Path) + gzw, ok := w.(c.GzipResponseWriter) + if ok { + w = gzw.ResponseWriter + h := w.Header() + h.Del("Content-Type") + h.Del("Content-Encoding") + } + req.URL.Path = "/static/favicon.ico" routes.StaticFile(w,req) return nil case "opensearch.xml": diff --git a/public/favicon.ico b/public/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..2ec3a9e09de7307c0d7abb2d86466bebf7df263d GIT binary patch literal 1086 zcmZQzU<5(|0R|wcz@Wjxz#zuJz@P!dKp~(AL>x$s81SvO+x%;7=Z9}~UH_5r3St7_ zdu>-8R8KHW{r`qZeBWxjj-ZRfWJ{W8-(0-U0G7UBxXb#9cWN{c9sDA|r!}#bl zKK`KALb5RG$H@v(gza<`5jmI!~6mY1DJYj cG{_C;X2A4 0 { h.Set("Content-Encoding", "gzip") - h.Set("Content-Length", strconv.FormatInt(file.GzipLength, 10)) + h.Set("Content-Length", file.StrGzipLength) io.Copy(w, bytes.NewReader(file.GzipData)) // Use w.Write instead? } else { h.Set("Content-Length", strconv.FormatInt(file.Length, 10)) // Avoid doing a type conversion every time?