xesite/cmd/site/rss.go

92 lines
2.4 KiB
Go
Raw Permalink Normal View History

package main
import (
2017-05-20 22:40:12 +00:00
"encoding/json"
"net/http"
"time"
"christine.website/internal"
2019-01-12 17:05:00 +00:00
"within.website/ln"
2019-03-27 14:18:52 +00:00
"within.website/ln/opname"
)
var bootTime = time.Now()
2019-03-27 14:18:52 +00:00
var etag = internal.Hash(bootTime.String(), IncrediblySecureSalt)
// IncrediblySecureSalt *******
const IncrediblySecureSalt = "hunter2"
func (s *Site) createFeed(w http.ResponseWriter, r *http.Request) {
2019-03-27 14:18:52 +00:00
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
}
2019-03-27 14:18:52 +00:00
w.Header().Set("Content-Type", "application/rss+xml")
err := s.rssFeed.WriteRss(w)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
2017-12-13 18:49:13 +00:00
ln.Error(r.Context(), err, ln.F{
"remote_addr": r.RemoteAddr,
"action": "generating_rss",
"uri": r.RequestURI,
"host": r.Host,
})
}
}
func (s *Site) createAtom(w http.ResponseWriter, r *http.Request) {
2019-03-27 14:18:52 +00:00
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
}
2019-03-27 14:18:52 +00:00
w.Header().Set("Content-Type", "application/atom+xml")
err := s.rssFeed.WriteAtom(w)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
2019-03-27 14:18:52 +00:00
ln.Error(ctx, err, ln.F{
"remote_addr": r.RemoteAddr,
2017-05-20 22:40:12 +00:00
"action": "generating_atom",
"uri": r.RequestURI,
"host": r.Host,
})
}
}
2019-03-27 14:18:52 +00:00
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
}
2017-05-20 22:40:12 +00:00
2019-03-27 14:18:52 +00:00
w.Header().Set("Content-Type", "application/json")
2017-05-20 22:40:12 +00:00
e := json.NewEncoder(w)
e.SetIndent("", "\t")
err := e.Encode(s.jsonFeed)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
2019-03-27 14:18:52 +00:00
ln.Error(ctx, err, ln.F{
2017-05-20 22:40:12 +00:00
"remote_addr": r.RemoteAddr,
"action": "generating_jsonfeed",
"uri": r.RequestURI,
"host": r.Host,
})
}
}