Add a simple letter favicon.

Disable Gzip for favicons, it doesn't make them smaller anyway.
Eliminate an allocation for static files.

Fixes #59
This commit is contained in:
Azareal 2019-07-23 20:34:44 +10:00
parent cb8e83ec79
commit ed68c6fc04
5 changed files with 28 additions and 13 deletions

View File

@ -32,6 +32,7 @@ type SFile struct {
Pos int64 Pos int64
Length int64 Length int64
GzipLength int64 GzipLength int64
StrGzipLength string
Mimetype string Mimetype string
Info os.FileInfo Info os.FileInfo
FormattedModTime string FormattedModTime string
@ -239,7 +240,7 @@ func (list SFileList) JSTmplInit() error {
hasher.Write(data) hasher.Write(data)
checksum := hex.EncodeToString(hasher.Sum(nil)) 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) DebugLogf("Added the '%s' static file.", path)
return nil 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) DebugLogf("Added the '%s' static file.", path)
return nil return nil
@ -317,7 +318,7 @@ func (list SFileList) Add(path string, prefix string) error {
hasher.Write(data) hasher.Write(data)
checksum := hex.EncodeToString(hasher.Sum(nil)) 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) DebugLogf("Added the '%s' static file", path)
return nil return nil

View File

@ -2290,8 +2290,9 @@ func (r *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, user c
gzw, ok := w.(c.GzipResponseWriter) gzw, ok := w.(c.GzipResponseWriter)
if ok { if ok {
w = gzw.ResponseWriter w = gzw.ResponseWriter
w.Header().Del("Content-Type") h := w.Header()
w.Header().Del("Content-Encoding") h.Del("Content-Type")
h.Del("Content-Encoding")
} }
counters.RouteViewCounter.Bump(142) counters.RouteViewCounter.Bump(142)
req.URL.Path += extraData req.URL.Path += extraData
@ -2306,8 +2307,14 @@ func (r *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, user c
counters.RouteViewCounter.Bump(144) counters.RouteViewCounter.Bump(144)
return routes.RobotsTxt(w,req) return routes.RobotsTxt(w,req)
case "favicon.ico": case "favicon.ico":
req.URL.Path = "/static"+req.URL.Path+extraData gzw, ok := w.(c.GzipResponseWriter)
//log.Print("req.URL.Path: ",req.URL.Path) 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) routes.StaticFile(w,req)
return nil return nil
case "opensearch.xml": case "opensearch.xml":

BIN
public/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -816,8 +816,9 @@ func (r *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, user c
gzw, ok := w.(c.GzipResponseWriter) gzw, ok := w.(c.GzipResponseWriter)
if ok { if ok {
w = gzw.ResponseWriter w = gzw.ResponseWriter
w.Header().Del("Content-Type") h := w.Header()
w.Header().Del("Content-Encoding") h.Del("Content-Type")
h.Del("Content-Encoding")
} }
counters.RouteViewCounter.Bump({{index .AllRouteMap "routes.UploadedFile" }}) counters.RouteViewCounter.Bump({{index .AllRouteMap "routes.UploadedFile" }})
req.URL.Path += extraData req.URL.Path += extraData
@ -832,8 +833,14 @@ func (r *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, user c
counters.RouteViewCounter.Bump({{index .AllRouteMap "routes.RobotsTxt"}}) counters.RouteViewCounter.Bump({{index .AllRouteMap "routes.RobotsTxt"}})
return routes.RobotsTxt(w,req) return routes.RobotsTxt(w,req)
case "favicon.ico": case "favicon.ico":
req.URL.Path = "/static"+req.URL.Path+extraData gzw, ok := w.(c.GzipResponseWriter)
//log.Print("req.URL.Path: ",req.URL.Path) 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) routes.StaticFile(w,req)
return nil return nil
case "opensearch.xml": case "opensearch.xml":

View File

@ -20,7 +20,7 @@ var cacheControlMaxAgeWeek = "max-age=" + strconv.Itoa(int(c.Week)) // TODO: Mak
func StaticFile(w http.ResponseWriter, r *http.Request) { func StaticFile(w http.ResponseWriter, r *http.Request) {
file, ok := c.StaticFiles.Get(r.URL.Path) file, ok := c.StaticFiles.Get(r.URL.Path)
if !ok { if !ok {
c.DebugLogf("Failed to find '%s'", r.URL.Path) // TODO: Use MicroNotFound? Might be better than the unneccessary overhead of sprintf //c.DebugLogf("Failed to find '%s'", r.URL.Path) // TODO: Use MicroNotFound? Might be better than the unneccessary overhead of sprintf
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
return return
} }
@ -43,7 +43,7 @@ func StaticFile(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") && file.GzipLength > 0 { if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") && file.GzipLength > 0 {
h.Set("Content-Encoding", "gzip") 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? io.Copy(w, bytes.NewReader(file.GzipData)) // Use w.Write instead?
} else { } else {
h.Set("Content-Length", strconv.FormatInt(file.Length, 10)) // Avoid doing a type conversion every time? h.Set("Content-Length", strconv.FormatInt(file.Length, 10)) // Avoid doing a type conversion every time?