added some Content-type header insertion in uncompressedfileroutes.

Changed how Get GetFresh work in the webLRU. Entries can now force loading from disk with LruEntry.Reload().
This commit is contained in:
dtookey 2022-07-27 12:17:41 -04:00
parent 3b9ecb168f
commit c3df1f3a85
2 changed files with 31 additions and 15 deletions

View File

@ -82,7 +82,7 @@ func createSingleFileServletFunction(route *FileRoute) VinegarHandlerFunction {
} else {
content = cache.Content
}
w.Header().Add(ContentTypeHeaderKey, cache.MimeType)
_, err := w.Write(content)
if err != nil {
panic(err)
@ -141,19 +141,18 @@ func createUncompressedFileServletFunction(route *FileRoute, basePattern string,
if !exists {
route.Cache.Put(stub, resourcePath)
entry, exists = route.Cache.Get(stub)
} else {
SendError(w, 404, "Oops! Something went wrong!")
return
}
if !exists {
SendError(w, 404, "Oops! Something went wrong.")
} else {
if exists {
w.Header().Add(ContentTypeHeaderKey, util.GuessMimetype(stub))
_, err := w.Write(entry.Content)
if err != nil {
panic(err)
}
return
} else {
SendError(w, 404, "Oops! Something went wrong.")
}
}
return fun

View File

@ -3,6 +3,7 @@ package vinegarUtil
import (
"errors"
"fmt"
"log"
"strings"
"time"
)
@ -58,8 +59,8 @@ func (l *Lru) Get(key string) (*LruEntry, bool) {
entry, exists := (*l.entries)[key]
if exists && entry.expires.Before(time.Now()) {
fmt.Println("Cache miss due to expired content")
l.Remove(key)
return nil, false
entry.Reload()
return entry, true
} else {
if exists {
entry.mostRecentAccess = time.Now()
@ -69,7 +70,10 @@ func (l *Lru) Get(key string) (*LruEntry, bool) {
}
func (l *Lru) GetFresh(key string) (*LruEntry, bool) {
l.Remove(key)
entry, exists := l.Get(key)
if exists {
entry.Reload()
}
return l.Get(key)
}
@ -152,14 +156,22 @@ func (l *Lru) recalcSize() {
l.currentSize = total
}
func (le *LruEntry) Reload() {
content, _ := GetDiskContent(le.path)
le.Content = *content
le.CompressedContent = *GZipBytes(content)
le.created = time.Now()
le.mostRecentAccess = le.created
le.expires = le.created.Add(time.Minute * DefaultCacheTimeInMinutes)
}
func (sfc *SingleFileCache) Get(key string) (*LruEntry, bool) {
return sfc.entry, true
}
func (sfc *SingleFileCache) GetFresh(key string) (*LruEntry, bool) {
entry := newLRUEntry(sfc.entry.path)
sfc.entry = entry
return entry, true
sfc.entry.Reload()
return sfc.entry, true
}
// Put THIS WILL DELETE ANYTHING YOU HAVE STORED IN THIS CACHE.
@ -201,6 +213,8 @@ func GuessMimetype(filePath string) string {
dotIndex := strings.LastIndex(filePath, ".")
ext := filePath[dotIndex+1:]
switch ext {
case "css":
return "text/css"
case "htm":
case "html":
return "text/html"
@ -209,13 +223,16 @@ func GuessMimetype(filePath string) string {
case "jpg":
case "jpeg":
return "image/jpeg"
case "js":
return "text/javascript"
case "json":
return "application/json"
case "png":
return "image/png"
case "svg":
return "image/svg+xml"
case "css":
return "text/css"
default:
log.Default().Printf("[WARN] '%s' is unrecognized MimeType. Returning as [application/octet-stream]. Please use VinegarServlet.AddMimeType() to define the appropriate response for this file extension.", ext)
return "application/octet-stream"
}
return "application/octet-stream"