aboutsummaryrefslogtreecommitdiff
path: root/cmd/site/rss.go
blob: cd99bde4223443e52622c44527212b5fcff54167 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main

import (
	"encoding/json"
	"net/http"
	"time"

	"christine.website/cmd/site/internal"
	"within.website/ln"
	"within.website/ln/opname"
)

var bootTime = time.Now()
var etag = internal.Hash(bootTime.String(), IncrediblySecureSalt)

// IncrediblySecureSalt *******
const IncrediblySecureSalt = "hunter2"

func (s *Site) createFeed(w http.ResponseWriter, r *http.Request) {
	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)
		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) {
	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(ctx, err, ln.F{
			"remote_addr": r.RemoteAddr,
			"action":      "generating_atom",
			"uri":         r.RequestURI,
			"host":        r.Host,
		})
	}
}

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(ctx, err, ln.F{
			"remote_addr": r.RemoteAddr,
			"action":      "generating_jsonfeed",
			"uri":         r.RequestURI,
			"host":        r.Host,
		})
	}
}