package ui import ( "html/template" "log" "net/http" "time" "git.streifling.com/jason/rss" "streifling.com/jason/cpolis/cmd/data" ) func ShowRSS(db *data.DB, title, link, desc string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { channel := &rss.Channel{ Title: title, Link: link, Description: desc, Items: make([]*rss.Item, 0), } articles, err := db.GetCertainArticles(true) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } for _, article := range articles { tags, err := db.GetArticleTags(article.ID) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } tagNames := make([]string, 0) for _, tag := range tags { tagNames = append(tagNames, tag.Name) } user, err := db.GetUser(article.AuthorID) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } channel.Items = append(channel.Items, &rss.Item{ Title: article.Title, Author: user.FirstName + user.LastName, PubDate: article.Created.Format(time.RFC1123Z), Description: article.Desc, Content: &rss.Content{Value: article.Content}, Categories: tagNames, }) } feed := rss.NewFeed() feed.Channels = append(feed.Channels, channel) rss, err := feed.ToXML() if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } files := []string{"web/templates/index.html", "web/templates/feed.rss"} tmpl, err := template.ParseFiles(files...) template.Must(tmpl, err).Execute(w, rss) } }