/
feed_handlers.go
69 lines (57 loc) · 1.74 KB
/
feed_handlers.go
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
package handlers
import (
"io"
"net/http"
"strings"
"time"
"github.com/go-chi/chi/v5"
"github.com/saaste/bookmark-manager/feeds"
)
type FeedType int
const (
FeedTypeAtom FeedType = iota
FeedTypeRSS
FeedTypeJSON
)
func (h *Handler) HandleFeed(w http.ResponseWriter, r *http.Request) {
isAuthenticated := h.isAuthenticated(w, r)
tags := chi.URLParam(r, "tags")
q := r.URL.Query().Get("q")
bookmarkResult, err := h.getBookmarksWithPagination(isAuthenticated, q, tags, 1, 20)
if err != nil {
h.internalServerError(w, "Failed to fetch bookmarks", err)
return
}
updated := time.Now()
if len(bookmarkResult.Bookmarks) > 0 {
updated = bookmarkResult.Bookmarks[0].Created
}
feedInfo := feeds.FeedInfo{
SiteName: h.appConf.SiteName,
Description: h.appConf.Description,
BaseURL: h.appConf.BaseURL,
CurrentURL: h.getCurrentURL(r),
AuthorName: h.appConf.AuthorName,
AuthorEmail: h.appConf.AuthorEmail,
}
var content string
switch true {
case strings.HasSuffix(r.URL.Path, "atom.xml"):
w.Header().Set("Content-Type", "application/atom+xml; charset=utf-8")
content = feeds.ToAtom(feedInfo, bookmarkResult.Bookmarks)
case strings.HasSuffix(r.URL.Path, "rss.xml"):
w.Header().Set("Content-Type", "application/rss+xml; charset=utf-8")
content = feeds.ToRSS(feedInfo, bookmarkResult.Bookmarks)
case strings.HasSuffix(r.URL.Path, "feed.json"):
w.Header().Set("Content-Type", "application/json; charset=utf-8")
content, err = feeds.ToJSON(feedInfo, bookmarkResult.Bookmarks)
default:
http.Error(w, "Invalid feed type", http.StatusBadRequest)
}
if err != nil {
h.internalServerError(w, "Generating feed failed", err)
return
}
w.Header().Set("Last-Modified", updated.Format(time.RFC1123))
io.WriteString(w, content)
}