diff options
| author | Xe Iaso <me@xeiaso.net> | 2024-02-24 16:07:38 -0500 |
|---|---|---|
| committer | Xe Iaso <me@xeiaso.net> | 2024-02-24 16:07:38 -0500 |
| commit | 774fccbe94f9a252c72d5394e5cbd027aaaebf64 (patch) | |
| tree | f665950a197fe4cad115b9f07557acfa887e486a /cmd | |
| parent | 93f6421e1d4e9ce9a4bcf1a55815fed3e2cee962 (diff) | |
| download | xesite-774fccbe94f9a252c72d5394e5cbd027aaaebf64.tar.xz xesite-774fccbe94f9a252c72d5394e5cbd027aaaebf64.zip | |
wire up the protofeed call
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/xesite/api.go | 76 | ||||
| -rw-r--r-- | cmd/xesite/main.go | 5 |
2 files changed, 80 insertions, 1 deletions
diff --git a/cmd/xesite/api.go b/cmd/xesite/api.go index 958396f..3a4eadb 100644 --- a/cmd/xesite/api.go +++ b/cmd/xesite/api.go @@ -2,6 +2,8 @@ package main import ( "context" + "encoding/json" + "io/fs" "os" "os/exec" "runtime" @@ -10,8 +12,10 @@ import ( "github.com/twitchtv/twirp" "google.golang.org/protobuf/types/known/emptypb" "google.golang.org/protobuf/types/known/timestamppb" + "xeiaso.net/v4/internal/jsonfeed" "xeiaso.net/v4/internal/lume" "xeiaso.net/v4/pb" + "xeiaso.net/v4/pb/external/protofeed" ) var denoVersion string @@ -50,3 +54,75 @@ func (ms *MetaServer) Metadata(ctx context.Context, _ *emptypb.Empty) (*pb.Build return result, nil } + +type FeedServer struct { + fs *lume.FS +} + +func (f *FeedServer) Get(ctx context.Context, _ *emptypb.Empty) (*protofeed.Feed, error) { + data, err := fs.ReadFile(f.fs, "blog.json") + if err != nil { + return nil, twirp.InternalErrorf("can't read blog.json: %w", err) + } + + var feed jsonfeed.Feed + + if err := json.Unmarshal(data, &feed); err != nil { + return nil, twirp.InternalErrorf("can't unmarshal blog.json: %w", err) + } + + var result protofeed.Feed + + result.Title = feed.Title + result.HomePageUrl = feed.HomePageURL + result.FeedUrl = feed.FeedURL + result.Description = feed.Description + result.UserComment = feed.UserComment + result.Icon = feed.Icon + result.Favicon = feed.Favicon + result.Expired = feed.Expired + result.Language = feed.Language + result.Items = make([]*protofeed.Item, len(feed.Items)) + result.Authors = make([]*protofeed.Author, len(feed.Authors)) + + for i, item := range feed.Items { + var atts []*protofeed.Attachment + for _, att := range item.Attachments { + atts = append(atts, &protofeed.Attachment{ + Url: att.URL, + MimeType: att.MIMEType, + Title: att.Title, + SizeInBytes: att.SizeInBytes, + DurationInSeconds: att.DurationInSeconds, + }) + } + + var authors []*protofeed.Author + for _, author := range item.Authors { + authors = append(authors, &protofeed.Author{ + Name: author.Name, + Url: author.URL, + Avatar: author.Avatar, + }) + } + + result.Items[i] = &protofeed.Item{ + Id: item.ID, + Url: item.URL, + ExternalUrl: item.ExternalURL, + Title: item.Title, + ContentHtml: item.ContentHTML, + ContentText: item.ContentText, + Summary: item.Summary, + Image: item.Image, + BannerImage: item.BannerImage, + DatePublished: timestamppb.New(item.DatePublished), + DateModified: timestamppb.New(item.DateModified), + Tags: item.Tags, + Authors: authors, + Attachments: atts, + } + } + + return &result, nil +} diff --git a/cmd/xesite/main.go b/cmd/xesite/main.go index c470855..d994431 100644 --- a/cmd/xesite/main.go +++ b/cmd/xesite/main.go @@ -11,9 +11,9 @@ import ( "path/filepath" "github.com/donatj/hmacsig" + swaggerui "github.com/esceer/todo/swagger-ui" "github.com/facebookgo/flagenv" _ "github.com/joho/godotenv/autoload" - "github.com/esceer/todo/swagger-ui" "github.com/twitchtv/twirp" "xeiaso.net/v4/internal" "xeiaso.net/v4/internal/lume" @@ -87,6 +87,9 @@ func main() { ms := pb.NewMetaServer(&MetaServer{fs}, twirp.WithServerPathPrefix("/api")) mux.Handle(ms.PathPrefix(), ms) + fsrv := pb.NewFeedServer(&FeedServer{fs}, twirp.WithServerPathPrefix("/api")) + mux.Handle(fsrv.PathPrefix(), fsrv) + mux.HandleFunc("/blog.atom", func(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/blog.rss", http.StatusMovedPermanently) }) |
