From d0ff1b2d04fc0caf57d99f1d34ce13cf4aae6a1a Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Wed, 27 Mar 2019 07:18:52 -0700 Subject: reorg: phase 1 --- cmd/site/rss.go | 47 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) (limited to 'cmd/site/rss.go') diff --git a/cmd/site/rss.go b/cmd/site/rss.go index 2f319b2..5b2c73a 100644 --- a/cmd/site/rss.go +++ b/cmd/site/rss.go @@ -5,19 +5,29 @@ import ( "net/http" "time" + "christine.website/internal" "within.website/ln" + "within.website/ln/opname" ) var bootTime = time.Now() -var etag = Hash(bootTime.String(), IncrediblySecureSalt) +var etag = internal.Hash(bootTime.String(), IncrediblySecureSalt) // IncrediblySecureSalt ******* const IncrediblySecureSalt = "hunter2" func (s *Site) createFeed(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/rss+xml") - w.Header().Set("ETag", "W/"+Hash(bootTime.String(), IncrediblySecureSalt)) + ctx := opname.With(r.Context(), "rss-feed") + fetag := "W/" + internal.Hash(bootTime.String(), IncrediblySecureSalt) + w.Header().Set("ETag", fetag) + + if r.Header.Get("If-None-Match") == fetag { + http.Error(w, "Cached data OK", http.StatusNotModified) + ln.Log(ctx, ln.Info("cache hit")) + return + } + w.Header().Set("Content-Type", "application/rss+xml") err := s.rssFeed.WriteRss(w) if err != nil { http.Error(w, "Internal server error", http.StatusInternalServerError) @@ -31,13 +41,21 @@ func (s *Site) createFeed(w http.ResponseWriter, r *http.Request) { } func (s *Site) createAtom(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/atom+xml") - w.Header().Set("ETag", "W/"+Hash(bootTime.String(), IncrediblySecureSalt)) + ctx := opname.With(r.Context(), "atom-feed") + fetag := "W/" + internal.Hash(bootTime.String(), IncrediblySecureSalt) + w.Header().Set("ETag", fetag) + + if r.Header.Get("If-None-Match") == fetag { + http.Error(w, "Cached data OK", http.StatusNotModified) + ln.Log(ctx, ln.Info("cache hit")) + return + } + w.Header().Set("Content-Type", "application/atom+xml") err := s.rssFeed.WriteAtom(w) if err != nil { http.Error(w, "Internal server error", http.StatusInternalServerError) - ln.Error(r.Context(), err, ln.F{ + ln.Error(ctx, err, ln.F{ "remote_addr": r.RemoteAddr, "action": "generating_atom", "uri": r.RequestURI, @@ -46,16 +64,25 @@ func (s *Site) createAtom(w http.ResponseWriter, r *http.Request) { } } -func (s *Site) createJsonFeed(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json") - w.Header().Set("ETag", Hash(bootTime.String(), IncrediblySecureSalt)) +func (s *Site) createJSONFeed(w http.ResponseWriter, r *http.Request) { + ctx := opname.With(r.Context(), "atom-feed") + fetag := "W/" + internal.Hash(bootTime.String(), IncrediblySecureSalt) + w.Header().Set("ETag", fetag) + + if r.Header.Get("If-None-Match") == fetag { + http.Error(w, "Cached data OK", http.StatusNotModified) + ln.Log(ctx, ln.Info("cache hit")) + return + } + + w.Header().Set("Content-Type", "application/json") e := json.NewEncoder(w) e.SetIndent("", "\t") err := e.Encode(s.jsonFeed) if err != nil { http.Error(w, "Internal server error", http.StatusInternalServerError) - ln.Error(r.Context(), err, ln.F{ + ln.Error(ctx, err, ln.F{ "remote_addr": r.RemoteAddr, "action": "generating_jsonfeed", "uri": r.RequestURI, -- cgit v1.2.3 From 7e6a1cbd5841b094a7e168f8500075443f31e8e0 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Wed, 27 Mar 2019 07:31:57 -0700 Subject: make logging cleaner, etag more --- cmd/site/rss.go | 1 - 1 file changed, 1 deletion(-) (limited to 'cmd/site/rss.go') diff --git a/cmd/site/rss.go b/cmd/site/rss.go index 5b2c73a..e03e07f 100644 --- a/cmd/site/rss.go +++ b/cmd/site/rss.go @@ -75,7 +75,6 @@ func (s *Site) createJSONFeed(w http.ResponseWriter, r *http.Request) { return } - w.Header().Set("Content-Type", "application/json") e := json.NewEncoder(w) e.SetIndent("", "\t") -- cgit v1.2.3