From 268fcf7e6b671d4959a12111d5abf553bf0a201b Mon Sep 17 00:00:00 2001 From: Mitchell Riedstra Date: Sun, 24 Oct 2021 12:52:38 -0400 Subject: Redis caching. Linter config and cleanup. --- cmd/server/feed.go | 48 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 15 deletions(-) (limited to 'cmd/server/feed.go') diff --git a/cmd/server/feed.go b/cmd/server/feed.go index c478365..880eeb4 100644 --- a/cmd/server/feed.go +++ b/cmd/server/feed.go @@ -16,8 +16,8 @@ import ( ) type Author struct { - Name string `xml:"name"` // Required - Uri string `xml:"uri,omitempty"` + Name string `xml:"name"` // Required + Uri string `xml:"uri,omitempty"` //nolint:golint,stylecheck Email string `xml:"email,omitempty"` } @@ -37,7 +37,7 @@ type Content struct { type Entry struct { // Spec requires this, autogenerated from Title and updated if otherwise // left empty - Id string `xml:"id"` + Id string `xml:"id"` //nolint:golint,stylecheck Title string `xml:"title"` // Required Updated *time.Time `xml:"updated"` // Required @@ -54,12 +54,13 @@ func (i Entry) MarshalXML(e *xml.Encoder, start xml.StartElement) error { if i.Title == "" { errs = append(errs, "Title Cannot be empty") } + if i.Updated == nil { errs = append(errs, "Updated cannot be nil") } if len(errs) > 0 { - return errors.New(strings.Join(errs, ",")) + return errors.New(strings.Join(errs, ",")) //nolint:goerr113 } if i.Id == "" { @@ -71,6 +72,7 @@ func (i Entry) MarshalXML(e *xml.Encoder, start xml.StartElement) error { return e.EncodeElement(i2, start) } +//nolint:stylecheck,golint type Atom struct { Ns string `xml:"xmlns,attr"` Title string `xml:"title"` // Required @@ -87,18 +89,21 @@ func (a Atom) MarshalXML(e *xml.Encoder, start xml.StartElement) error { a.Ns = "http://www.w3.org/2005/Atom" errs := []string{} + if a.Id == "" { errs = append(errs, "ID Cannot be empty") } + if a.Author.Name == "" { errs = append(errs, "Author Name cannot be empty") } + if a.Updated == nil { errs = append(errs, "Updated cannot be empty") } if len(errs) > 0 { - return errors.New(strings.Join(errs, ",")) + return errors.New(strings.Join(errs, ",")) //nolint:goerr113 } start.Name = xml.Name{Local: "feed"} @@ -114,12 +119,15 @@ func (a Atom) MarshalXML(e *xml.Encoder, start xml.StartElement) error { // Relevant query parameters are: // // "content" if unset, or set to false content is omitted from the feed -// "limit=n" stop at "n" and return the feed +// "limit=n" stop at "n" and return the feed. // -func (a *App) FeedHandler(w http.ResponseWriter, r *http.Request) { +func (a *App) FeedHandler(w http.ResponseWriter, r *http.Request) { //nolint:funlen vars := mux.Vars(r) - var addContent bool - var limit int + + var ( + addContent bool + limit int + ) if _, ok := r.URL.Query()["content"]; ok { if r.URL.Query().Get("content") != "false" { @@ -137,20 +145,24 @@ func (a *App) FeedHandler(w http.ResponseWriter, r *http.Request) { tag, ok := vars["tag"] if !ok { http.Error(w, "Tag not found or supplied", http.StatusNotFound) + return } p := page.NewPage("index") + index, err := p.Index() if err != nil { log.Println(err) http.Error(w, "Internal server error", http.StatusInternalServerError) + return } pages, ok := index[tag] if !ok { http.Error(w, "Invalid tag", http.StatusNotFound) + return } @@ -158,6 +170,7 @@ func (a *App) FeedHandler(w http.ResponseWriter, r *http.Request) { for _, p := range dateless { log.Printf("Warning, page %s has no Date field. Skipping inclusion on feed", p) } + pages.SortDate() feed := &Atom{ @@ -180,22 +193,24 @@ func (a *App) FeedHandler(w http.ResponseWriter, r *http.Request) { } content := &bytes.Buffer{} + err := p.Render(content) if err != nil { log.Println(err) http.Error(w, "Internal server error", http.StatusInternalServerError) + return } entry := Entry{ Title: p.Title, Updated: &p.Date.Time, - Links: []Link{Link{Href: strings.Join([]string{a.SiteURL, p.Path()}, "/")}}, + Links: []Link{{Href: strings.Join([]string{a.SiteURL, p.Path()}, "/")}}, } if p.AuthorName != "" { entry.Author = &Author{ - Name: p.AuthorName, + Name: p.AuthorName, } if p.AuthorEmail != "" { entry.Author.Email = p.AuthorEmail @@ -207,13 +222,18 @@ func (a *App) FeedHandler(w http.ResponseWriter, r *http.Request) { } entries = append(entries, entry) - } feed.Entries = entries w.Header().Add("Content-type", "application/xml") - w.Write([]byte(xml.Header)) + + _, err = w.Write([]byte(xml.Header)) + if err != nil { + log.Println("Writing xml: ", err) + + return + } enc := xml.NewEncoder(w) enc.Indent("", " ") @@ -224,6 +244,4 @@ func (a *App) FeedHandler(w http.ResponseWriter, r *http.Request) { // Headers probably already sent, but we'll try anyway http.Error(w, "Internal server error", http.StatusInternalServerError) } - - return } -- cgit v1.2.3