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

View File

@ -3,6 +3,7 @@ package vinegarUtil
import ( import (
"errors" "errors"
"fmt" "fmt"
"log"
"strings" "strings"
"time" "time"
) )
@ -58,8 +59,8 @@ func (l *Lru) Get(key string) (*LruEntry, bool) {
entry, exists := (*l.entries)[key] entry, exists := (*l.entries)[key]
if exists && entry.expires.Before(time.Now()) { if exists && entry.expires.Before(time.Now()) {
fmt.Println("Cache miss due to expired content") fmt.Println("Cache miss due to expired content")
l.Remove(key) entry.Reload()
return nil, false return entry, true
} else { } else {
if exists { if exists {
entry.mostRecentAccess = time.Now() entry.mostRecentAccess = time.Now()
@ -69,7 +70,10 @@ func (l *Lru) Get(key string) (*LruEntry, bool) {
} }
func (l *Lru) GetFresh(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) return l.Get(key)
} }
@ -152,14 +156,22 @@ func (l *Lru) recalcSize() {
l.currentSize = total 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) { func (sfc *SingleFileCache) Get(key string) (*LruEntry, bool) {
return sfc.entry, true return sfc.entry, true
} }
func (sfc *SingleFileCache) GetFresh(key string) (*LruEntry, bool) { func (sfc *SingleFileCache) GetFresh(key string) (*LruEntry, bool) {
entry := newLRUEntry(sfc.entry.path) sfc.entry.Reload()
sfc.entry = entry return sfc.entry, true
return entry, true
} }
// Put THIS WILL DELETE ANYTHING YOU HAVE STORED IN THIS CACHE. // Put THIS WILL DELETE ANYTHING YOU HAVE STORED IN THIS CACHE.
@ -201,6 +213,8 @@ func GuessMimetype(filePath string) string {
dotIndex := strings.LastIndex(filePath, ".") dotIndex := strings.LastIndex(filePath, ".")
ext := filePath[dotIndex+1:] ext := filePath[dotIndex+1:]
switch ext { switch ext {
case "css":
return "text/css"
case "htm": case "htm":
case "html": case "html":
return "text/html" return "text/html"
@ -209,13 +223,16 @@ func GuessMimetype(filePath string) string {
case "jpg": case "jpg":
case "jpeg": case "jpeg":
return "image/jpeg" return "image/jpeg"
case "js":
return "text/javascript"
case "json":
return "application/json"
case "png": case "png":
return "image/png" return "image/png"
case "svg": case "svg":
return "image/svg+xml" return "image/svg+xml"
case "css":
return "text/css"
default: 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"
} }
return "application/octet-stream" return "application/octet-stream"