diff options
author | Ben Morrison <ben@gbmor.dev> | 2019-05-26 01:48:12 -0400 |
---|---|---|
committer | Ben Morrison <ben@gbmor.dev> | 2019-05-26 02:05:59 -0400 |
commit | a6f47c07100be337f32325792990e58f5f4a3506 (patch) | |
tree | 309a35b47d4a900a9e02bc8bf48acb8296e10d93 /handlers.go | |
parent | e1e68aa2ba2992543603cfd5a4e1fa2e4eaeeb72 (diff) | |
download | getwtxt-a6f47c07100be337f32325792990e58f5f4a3506.tar.gz |
simplified indexHandler to reference staticCache
sending ETag with all GET responses: sha256 of raw bytes
Diffstat (limited to 'handlers.go')
-rw-r--r-- | handlers.go | 25 |
1 files changed, 11 insertions, 14 deletions
diff --git a/handlers.go b/handlers.go index 1e01619..b25901d 100644 --- a/handlers.go +++ b/handlers.go @@ -3,9 +3,7 @@ package main import ( "crypto/sha256" "fmt" - "log" "net/http" - "os" "strings" "github.com/gorilla/mux" @@ -14,13 +12,9 @@ import ( // handles "/" func indexHandler(w http.ResponseWriter, r *http.Request) { - // Stat the index template to get the mod time - var etag string - if indextmpl, err := os.Stat("assets/tmpl/index.html"); err != nil { - log.Printf("Couldn't stat index template for ETag ... %v\n", err) - } else { - etag = fmt.Sprintf("%x", sha256.Sum256([]byte(indextmpl.ModTime().String()))) - } + pingAssets() + + etag := fmt.Sprintf("%x", sha256.Sum256([]byte(staticCache.indexMod.String()))) // Take the hex-encoded sha256 sum of the index template's mod time // to send as an ETag. If an error occured when grabbing the file info, @@ -28,11 +22,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("ETag", "\""+etag+"\"") w.Header().Set("Content-Type", htmlutf8) - // Pass the confObj.Instance data to the template, - // then send it to the client. - confObj.Mu.RLock() - err := tmpls.ExecuteTemplate(w, "index.html", confObj.Instance) - confObj.Mu.RUnlock() + _, err := w.Write(staticCache.index) if err != nil { log500(w, r, err) return @@ -91,6 +81,8 @@ func apiEndpointHandler(w http.ResponseWriter, r *http.Request) { data = []byte("") } + etag := fmt.Sprintf("%x", sha256.Sum256(data)) + w.Header().Set("ETag", etag) w.Header().Set("Content-Type", txtutf8) _, err = w.Write(data) @@ -118,6 +110,8 @@ func apiTagsBaseHandler(w http.ResponseWriter, r *http.Request) { data := parseQueryOut(out) + etag := fmt.Sprintf("%x", sha256.Sum256(data)) + w.Header().Set("ETag", etag) w.Header().Set("Content-Type", txtutf8) _, err = w.Write(data) @@ -160,7 +154,10 @@ func apiTagsHandler(w http.ResponseWriter, r *http.Request) { data := parseQueryOut(out) + etag := fmt.Sprintf("%x", sha256.Sum256(data)) + w.Header().Set("ETag", etag) w.Header().Set("Content-Type", txtutf8) + _, err = w.Write(data) if err != nil { log500(w, r, err) |