From 1c6c1f1597b71f4d4f3a1722655b8864c0d33e6d Mon Sep 17 00:00:00 2001 From: Mitchell Riedstra Date: Sun, 11 Jul 2021 11:55:47 -0400 Subject: Initial banging away at adding an Atom feed to the site --- .gitignore | 2 - cmd/server/feed.go | 183 +++++++++++++++++++++++++++++++++++++++++++++++++ cmd/server/handlers.go | 8 +++ cmd/server/main.go | 26 ++++++- go.mod | 1 + go.sum | 6 ++ page/atom.go | 31 +++++++++ page/page.go | 9 +++ page/time.go | 5 ++ 9 files changed, 266 insertions(+), 5 deletions(-) delete mode 100644 .gitignore create mode 100644 cmd/server/feed.go create mode 100644 page/atom.go diff --git a/.gitignore b/.gitignore deleted file mode 100644 index d050e4c..0000000 --- a/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -*site* -server diff --git a/cmd/server/feed.go b/cmd/server/feed.go new file mode 100644 index 0000000..f3ccd8c --- /dev/null +++ b/cmd/server/feed.go @@ -0,0 +1,183 @@ +package main + +import ( + "bytes" + "encoding/xml" + "errors" + "fmt" + "log" + "net/http" + "strings" + "time" + + "github.com/gorilla/mux" + "riedstra.dev/mitch/go-website/page" +) + +type Author struct { + Name string `xml:"name"` // Required + Uri string `xml:"uri,omitempty"` + Email string `xml:"email,omitempty"` +} + +type Link struct { + Href string `xml:"href,attr,omitempty"` + Rel string `xml:"rel,attr,omitempty"` + Type string `xml:"type,attr,omitempty"` + Title string `xml:"Title,attr,omitempty"` + Length string `xml:"Length,attr,omitempty"` +} + +type Content struct { + Type string `xml:"type,attr"` + Data []byte `xml:"chardata"` +} + +type Entry struct { + // Spec requires this, autogenerated from Title and updated if otherwise + // left empty + Id string `xml:"id"` + + Title string `xml:"title"` // Required + Updated *time.Time `xml:"updated"` // Required + Author *Author `xml:"author,omitempty"` + Published time.Time `xml:"published,omitempty"` + Links []Link `xml:"link,omitempty"` + Content Content `xml:"content,omitempty"` +} + +func (i Entry) MarshalXML(e *xml.Encoder, start xml.StartElement) error { + type Alias Entry + + errs := []string{} + if i.Title == "" { + errs = append(errs, "Title Cannot be empty") + } + if i.Updated == nil { + errs = append(errs, "Updated cannot be nil") + } + + if i.Id == "" { + i.Id = fmt.Sprintf("%s::%d", i.Title, i.Updated.Unix()) + } + + if len(errs) > 0 { + return errors.New(strings.Join(errs, ",")) + } + + return e.EncodeElement( + &struct { + *Alias + }{ + Alias: (*Alias)(&i), + }, start) +} + +type Atom struct { + XMLName xml.Name `xml:"feed"` + Ns string `xml:"xmlns,attr"` + Title string `xml:"title"` // Required + Id string `xml:"id"` // Required + Author Author `xml:"author,omitempty"` // Required + Updated *time.Time `xml:"updated"` // Required + Published *time.Time `xml:"published,omitempty"` + Subtitle string `xml:"subtitle,omitempty"` + Entries []Entry `xml:"entry"` +} + +func (a Atom) MarshalXML(e *xml.Encoder, start xml.StartElement) error { + type Alias Atom + + 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 e.EncodeElement( + &struct { + *Alias + }{ + Alias: (*Alias)(&a), + }, start) +} + +func (a *App) FeedHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + + 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 + } + + feed := &Atom{ + Author: a.Author, + Title: a.Title, + Id: a.FeedId, + Updated: &a.Updated.Time, + Subtitle: a.Description, + } + + entries := []Entry{} + + for _, p := range pages { + + if p.Date == nil { + log.Printf("Warning, page %s has no Date field. Skipping inclusion on feed", p) + continue + } + + content := &bytes.Buffer{} + err := p.Render(content) + if err != nil { + log.Println(err) + http.Error(w, "Internal server error", http.StatusInternalServerError) + return + } + + entries = append(entries, Entry{ + Title: p.Title, + Updated: &p.Date.Time, + Links: []Link{Link{Href: p.Path()}}, + Content: Content{Type: "html", Data: content.Bytes()}, + }) + } + + feed.Entries = entries + + enc := xml.NewEncoder(w) + enc.Indent("", " ") + + err = enc.Encode(feed) + if err != nil { + log.Println(err) + http.Error(w, "Internal server error", http.StatusInternalServerError) + } + + return +} diff --git a/cmd/server/handlers.go b/cmd/server/handlers.go index 60e0a35..02fb712 100644 --- a/cmd/server/handlers.go +++ b/cmd/server/handlers.go @@ -11,6 +11,13 @@ import ( type App struct { ReIndexPath string StaticDirectory string + + // Related to the Atom feed + Title string + Description string // aka, "subtitle" + Author Author + FeedId string + Updated page.PageTime } func (a *App) PageHandler(w http.ResponseWriter, r *http.Request) { @@ -48,6 +55,7 @@ func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) { rtr := mux.NewRouter() rtr.HandleFunc(a.ReIndexPath, a.RebuildIndexHandler) rtr.PathPrefix("/static/").Handler(a.StaticHandler()) + rtr.PathPrefix("/.feeds/{tag}").HandlerFunc(a.FeedHandler) rtr.PathPrefix("/").HandlerFunc(a.PageHandler) rtr.ServeHTTP(w, r) } diff --git a/cmd/server/main.go b/cmd/server/main.go index 9ec96da..b2a95c3 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -3,6 +3,7 @@ package main import ( "flag" "fmt" + "gopkg.in/yaml.v3" "log" "net/http" "os" @@ -18,6 +19,18 @@ func VersionPrint() { os.Exit(0) } +func loadConf(fn string) (*App, error) { + fh, err := os.Open(fn) + if err != nil { + return nil, err + } + dec := yaml.NewDecoder(fh) + + app := &App{} + err = dec.Decode(app) + return app, err +} + func main() { fl := flag.NewFlagSet("Website", flag.ExitOnError) listen := fl.String("l", "0.0.0.0:8001", "Listening address") @@ -36,11 +49,18 @@ func main() { log.Fatal(err) } - app := &App{ - ReIndexPath: *indexPath, - StaticDirectory: "static", + app, err := loadConf("conf.yml") + if err != nil { + log.Println(err) + app = &App{} } + app.ReIndexPath = *indexPath + app.StaticDirectory = "static" + + b, _ := yaml.Marshal(app) + os.Stderr.Write(b) + srv := &http.Server{ Handler: app, Addr: *listen, diff --git a/go.mod b/go.mod index efa317a..93083c0 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/go-kivik/kivik v2.0.0+incompatible github.com/go-kivik/kiviktest v2.0.0+incompatible // indirect github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 // indirect + github.com/gorilla/feeds v1.1.1 // indirect github.com/gorilla/mux v1.8.0 github.com/kr/pretty v0.1.0 // indirect github.com/pkg/errors v0.9.1 // indirect diff --git a/go.sum b/go.sum index 72f0eb6..f481937 100644 --- a/go.sum +++ b/go.sum @@ -12,10 +12,13 @@ github.com/go-kivik/kiviktest v2.0.0+incompatible h1:y1RyPHqWQr+eFlevD30Tr3ipiPC github.com/go-kivik/kiviktest v2.0.0+incompatible/go.mod h1:JdhVyzixoYhoIDUt6hRf1yAfYyaDa5/u9SDOindDkfQ= github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 h1:l5lAOZEym3oK3SQ2HBHWsJUfbNBiTXJDeW2QDxw9AQ0= github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/gorilla/feeds v1.1.1 h1:HwKXxqzcRNg9to+BbvJog4+f3s/xzvtZXICcQGutYfY= +github.com/gorilla/feeds v1.1.1/go.mod h1:Nk0jZrvPFZX1OBe5NPiddPw7CfwF6Q9eqzaBbaightA= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1 h1:VkoXIwSboBpnk99O/KFauAEILuNHv5DVFKZMBN/gUgw= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= @@ -37,13 +40,16 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV gitlab.com/flimzy/testy v0.3.0 h1:HbY+NAJjXWxRqX8X4yZ0Blr5t6Yxc2n5RYREGVkwFDw= gitlab.com/flimzy/testy v0.3.0/go.mod h1:YObF4cq711ubd/3U0ydRQQVz7Cnq/ChgJpVwNr/AJac= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20200925080053-05aa5d4ee321 h1:lleNcKRbcaC8MqgLwghIkzZ2JBQAb7QQ9MiwRt1BisA= golang.org/x/net v0.0.0-20200925080053-05aa5d4ee321/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= diff --git a/page/atom.go b/page/atom.go new file mode 100644 index 0000000..4a89117 --- /dev/null +++ b/page/atom.go @@ -0,0 +1,31 @@ +package page + +/* + +import ( + "github.com/gorilla/feeds" + "io" +) + +// Atom returns an Atom feed for all of the articles with the specified tag +func (p *Page) Atom(wr io.Writer, tag string) error { + index, err := p.Index() + if err != nil { + return err + } + + pages, ok := index[tag] + if !ok { + pages = PageList{} + } + + feed := &feeds.Feed{ + Title: "", + Link: &feeds.Link{Href: "http://jmoiron.net/blog"}, + Description: "discussion about tech, footie, photos", + Author: &feeds.Author{Name: "Jason Moiron", Email: "jmoiron@jmoiron.net"}, + Created: now, + } + +} +*/ diff --git a/page/page.go b/page/page.go index 82f46f2..ecd2268 100644 --- a/page/page.go +++ b/page/page.go @@ -25,6 +25,7 @@ package page import ( "bufio" "bytes" + "encoding/json" "fmt" "io" "log" @@ -45,6 +46,8 @@ type Page struct { Title string Head string Description string + AuthorName string + AuthorEmail string // Tags to apply to the page in question. Useful for Index() Tags map[string]interface{} Date *PageTime @@ -209,3 +212,9 @@ func (p *Page) RenderBody() (string, error) { func (p Page) String() string { return fmt.Sprintf("Page: %s", p.path) } + +// StringDetail prints a detailed string of the page +func (p Page) StringDetail() string { + b, _ := json.MarshalIndent(p, "", " ") + return fmt.Sprintf("Page: %s\n%s\n", p.path, b) +} diff --git a/page/time.go b/page/time.go index 958dc38..0374490 100644 --- a/page/time.go +++ b/page/time.go @@ -21,3 +21,8 @@ func (pt *PageTime) UnmarshalYAML(n *yaml.Node) error { pt.Time = t return nil } + +func (pt PageTime) MarshalYAML() (interface{}, error) { + s := pt.Time.Format(TimeFormat) + return s, nil +} -- cgit v1.2.3