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 | |
parent | e1e68aa2ba2992543603cfd5a4e1fa2e4eaeeb72 (diff) | |
download | getwtxt-a6f47c07100be337f32325792990e58f5f4a3506.tar.gz |
simplified indexHandler to reference staticCache
sending ETag with all GET responses: sha256 of raw bytes
-rw-r--r-- | cache.go | 2 | ||||
-rw-r--r-- | handlers.go | 25 | ||||
-rw-r--r-- | http.go | 5 | ||||
-rw-r--r-- | query.go | 4 |
4 files changed, 19 insertions, 17 deletions
diff --git a/cache.go b/cache.go index 0a22f55..a5531b0 100644 --- a/cache.go +++ b/cache.go @@ -143,7 +143,7 @@ func pullDatabase() { // Start with an empty Data struct. If // there's already one in the cache, pull // it and use it instead. - data := registry.NewUserData() + data := registry.NewUser() twtxtCache.Mu.RLock() if _, ok := twtxtCache.Reg[urls]; ok { data = twtxtCache.Reg[urls] 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) diff --git a/http.go b/http.go index fcce0a7..de9eb3b 100644 --- a/http.go +++ b/http.go @@ -22,8 +22,9 @@ func newCtxUserIP(ctx context.Context, r *http.Request) context.Context { uip = base[0] } - if _, ok := r.Header["X-Real-IP"]; ok { - proxied := r.Header["X-Real-IP"] + xRealIP := http.CanonicalHeaderKey("X-Real-IP") + if _, ok := r.Header[xRealIP]; ok { + proxied := r.Header[xRealIP] base = strings.Split(proxied[len(proxied)-1], ":") uip = base[0] } diff --git a/query.go b/query.go index 63e203c..17d7a36 100644 --- a/query.go +++ b/query.go @@ -1,6 +1,7 @@ package main import ( + "crypto/sha256" "fmt" "log" "net/http" @@ -107,7 +108,10 @@ func apiEndpointQuery(w http.ResponseWriter, r *http.Request) error { 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) return err |