diff options
| author | Mitchell Riedstra <mitch@riedstra.dev> | 2021-07-12 00:24:47 -0400 |
|---|---|---|
| committer | Mitchell Riedstra <mitch@riedstra.dev> | 2021-07-12 00:24:47 -0400 |
| commit | 3f47f136de6a79adedbe16329586944c6a90ee65 (patch) | |
| tree | 02eaad2174ac1630c06f77194fda832afc80aaf1 /cmd/server | |
| parent | 1c6c1f1597b71f4d4f3a1722655b8864c0d33e6d (diff) | |
| download | go-website-3f47f136de6a79adedbe16329586944c6a90ee65.tar.gz go-website-3f47f136de6a79adedbe16329586944c6a90ee65.tar.xz | |
Patch up the xml. Produces a usable feed now.
Diffstat (limited to 'cmd/server')
| -rw-r--r-- | cmd/server/feed.go | 39 | ||||
| -rw-r--r-- | cmd/server/handlers.go | 1 |
2 files changed, 20 insertions, 20 deletions
diff --git a/cmd/server/feed.go b/cmd/server/feed.go index f3ccd8c..278c706 100644 --- a/cmd/server/feed.go +++ b/cmd/server/feed.go @@ -41,9 +41,9 @@ type Entry struct { Title string `xml:"title"` // Required Updated *time.Time `xml:"updated"` // Required Author *Author `xml:"author,omitempty"` - Published time.Time `xml:"published,omitempty"` + Published *time.Time `xml:"published,omitempty"` Links []Link `xml:"link,omitempty"` - Content Content `xml:"content,omitempty"` + Content *Content `xml:"content,omitempty"` } func (i Entry) MarshalXML(e *xml.Encoder, start xml.StartElement) error { @@ -57,24 +57,20 @@ func (i Entry) MarshalXML(e *xml.Encoder, start xml.StartElement) error { errs = append(errs, "Updated cannot be nil") } + if len(errs) > 0 { + return errors.New(strings.Join(errs, ",")) + } + if i.Id == "" { i.Id = fmt.Sprintf("%s::%d", i.Title, i.Updated.Unix()) } - if len(errs) > 0 { - return errors.New(strings.Join(errs, ",")) - } + i2 := (*Alias)(&i) - return e.EncodeElement( - &struct { - *Alias - }{ - Alias: (*Alias)(&i), - }, start) + return e.EncodeElement(i2, start) } type Atom struct { - XMLName xml.Name `xml:"feed"` Ns string `xml:"xmlns,attr"` Title string `xml:"title"` // Required Id string `xml:"id"` // Required @@ -104,12 +100,11 @@ func (a Atom) MarshalXML(e *xml.Encoder, start xml.StartElement) error { return errors.New(strings.Join(errs, ",")) } - return e.EncodeElement( - &struct { - *Alias - }{ - Alias: (*Alias)(&a), - }, start) + start.Name = xml.Name{Local: "feed"} + + a2 := (*Alias)(&a) + + return e.EncodeElement(a2, start) } func (a *App) FeedHandler(w http.ResponseWriter, r *http.Request) { @@ -163,19 +158,23 @@ func (a *App) FeedHandler(w http.ResponseWriter, r *http.Request) { entries = append(entries, Entry{ Title: p.Title, Updated: &p.Date.Time, - Links: []Link{Link{Href: p.Path()}}, - Content: Content{Type: "html", Data: content.Bytes()}, + Links: []Link{Link{Href: strings.Join([]string{a.SiteURL, p.Path()}, "/")}}, + // Content: Content{Type: "html", Data: content.Bytes()}, }) } feed.Entries = entries + w.Header().Add("Content-type", "application/xml") + w.Write([]byte(xml.Header)) + enc := xml.NewEncoder(w) enc.Indent("", " ") err = enc.Encode(feed) if err != nil { log.Println(err) + // Headers probably already sent, but we'll try anyway http.Error(w, "Internal server error", http.StatusInternalServerError) } diff --git a/cmd/server/handlers.go b/cmd/server/handlers.go index 02fb712..a54e5ee 100644 --- a/cmd/server/handlers.go +++ b/cmd/server/handlers.go @@ -16,6 +16,7 @@ type App struct { Title string Description string // aka, "subtitle" Author Author + SiteURL string FeedId string Updated page.PageTime } |
