diff options
author | Ben Morrison <ben@gbmor.dev> | 2019-05-23 02:16:41 -0400 |
---|---|---|
committer | Ben Morrison <ben@gbmor.dev> | 2019-05-23 02:16:41 -0400 |
commit | 2b0d4a52363b8683a594066bf78786fca3ac881a (patch) | |
tree | 7b3a0f17e4e35d59600dea378179f6fbc4dda9d1 | |
parent | cd635e6c6b009d6c9d6943ea7c850ca740172b65 (diff) | |
download | getwtxt-2b0d4a52363b8683a594066bf78786fca3ac881a.tar.gz |
caching some static assets
-rw-r--r-- | cache.go | 51 | ||||
-rw-r--r-- | handlers.go | 24 | ||||
-rw-r--r-- | init.go | 13 |
3 files changed, 68 insertions, 20 deletions
diff --git a/cache.go b/cache.go index 2ed21f3..214d185 100644 --- a/cache.go +++ b/cache.go @@ -1,8 +1,10 @@ package main import ( + "io/ioutil" "log" "net" + "os" "strings" "time" @@ -185,3 +187,52 @@ func pullDatabase() { log.Printf("Error while pulling DB into registry cache: %v\n", err) } } + +// pingAssets checks if the local static assets +// need to be re-cached. If they do, they are +// pulled back into memory from disk. +func pingAssets() { + + cssStat, err := os.Stat("assets/style.css") + if err != nil { + log.Printf("%v\n", err) + } + /* + indexStat, err := os.Stat("assets/tmpl/index.html") + if err != nil { + log.Printf("%v\n", err) + } + + indexMod := staticCache.indexMod*/ + cssMod := staticCache.cssMod + /* + if !indexMod.Equal(indexStat.ModTime()) { + var err error + tmpls, err = template.ParseFiles("assets/tmpl/index.html") + if err != nil { + log.Printf("%v\n", err) + } + buf := bytes.NewBuffer(staticCache.index) + + confObj.Mu.RLock() + err = tmpls.ExecuteTemplate(buf, "index.html", confObj.Instance) + confObj.Mu.RUnlock() + if err != nil { + log.Printf("%v\n", err) + } + + staticCache.index = buf.Bytes() + staticCache.indexMod = indexStat.ModTime() + } + */ + if !cssMod.Equal(cssStat.ModTime()) { + + css, err := ioutil.ReadFile("assets/style.css") + if err != nil { + log.Printf("%v\n", err) + } + + staticCache.css = css + staticCache.cssMod = cssStat.ModTime() + } +} diff --git a/handlers.go b/handlers.go index 7f05f8f..1e01619 100644 --- a/handlers.go +++ b/handlers.go @@ -3,7 +3,6 @@ package main import ( "crypto/sha256" "fmt" - "io/ioutil" "log" "net/http" "os" @@ -175,30 +174,15 @@ func apiTagsHandler(w http.ResponseWriter, r *http.Request) { // files aren't served directly. func cssHandler(w http.ResponseWriter, r *http.Request) { - // read the raw bytes of the stylesheet - css, err := ioutil.ReadFile("assets/style.css") - if err != nil { - if os.IsNotExist(err) { - log404(w, r, err) - return - } - log500(w, r, err) - return - } - - // Get the mod time for the etag header - stat, err := os.Stat("assets/style.css") - if err != nil { - log.Printf("Couldn't stat CSS file to send ETag header: %v\n", err) - } - // Sending the sha256 sum of the modtime in hexadecimal for the ETag header - etag := fmt.Sprintf("%x", sha256.Sum256([]byte(stat.ModTime().String()))) + etag := fmt.Sprintf("%x", sha256.Sum256([]byte(staticCache.cssMod.String()))) w.Header().Set("ETag", "\""+etag+"\"") w.Header().Set("Content-Type", cssutf8) - n, err := w.Write(css) + pingAssets() + + n, err := w.Write(staticCache.css) if err != nil || n == 0 { log500(w, r, err) return diff --git a/init.go b/init.go index 8e7fe2f..25beeed 100644 --- a/init.go +++ b/init.go @@ -42,6 +42,19 @@ var twtxtCache = registry.NewIndex() // remote registry listing var remoteRegistries = &RemoteRegistries{} +// static assets cache +var staticCache = &struct { + index []byte + indexMod time.Time + css []byte + cssMod time.Time +}{ + index: nil, + indexMod: time.Time{}, + css: nil, + cssMod: time.Time{}, +} + func initGetwtxt() { checkFlags() titleScreen() |