diff options
| author | Christine Dodrill <me@christine.website> | 2019-03-27 07:47:10 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-03-27 07:47:10 -0700 |
| commit | dcfac4d41fc50d2cf06ee3df4e03dfd66dd7e0f4 (patch) | |
| tree | 7b1f1515d7e8297de14f8a2ebdfa8f4f3ae332df /cmd/site/rss.go | |
| parent | 20c08e68d1218475d6c6f3ca8ae718e1f508c696 (diff) | |
| parent | bce017f56f2413872a3a904067d6d19d173a9904 (diff) | |
| download | xesite-dcfac4d41fc50d2cf06ee3df4e03dfd66dd7e0f4.tar.xz xesite-dcfac4d41fc50d2cf06ee3df4e03dfd66dd7e0f4.zip | |
Merge pull request #18 from Xe/reorg
Reorg
Diffstat (limited to 'cmd/site/rss.go')
| -rw-r--r-- | cmd/site/rss.go | 46 |
1 files changed, 36 insertions, 10 deletions
diff --git a/cmd/site/rss.go b/cmd/site/rss.go index 2f319b2..e03e07f 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,24 @@ 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, |
