diff options
| author | Christine Dodrill <me@christine.website> | 2019-03-27 07:18:52 -0700 |
|---|---|---|
| committer | Christine Dodrill <me@christine.website> | 2019-03-27 07:18:52 -0700 |
| commit | d0ff1b2d04fc0caf57d99f1d34ce13cf4aae6a1a (patch) | |
| tree | 29ae006563834338e663452e6f810561596582ac /cmd/site/rss.go | |
| parent | 20c08e68d1218475d6c6f3ca8ae718e1f508c696 (diff) | |
| download | xesite-d0ff1b2d04fc0caf57d99f1d34ce13cf4aae6a1a.tar.xz xesite-d0ff1b2d04fc0caf57d99f1d34ce13cf4aae6a1a.zip | |
reorg: phase 1
Diffstat (limited to 'cmd/site/rss.go')
| -rw-r--r-- | cmd/site/rss.go | 47 |
1 files changed, 37 insertions, 10 deletions
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, |
