aboutsummaryrefslogtreecommitdiff
path: root/cmd/xesite/api.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/xesite/api.go')
-rw-r--r--cmd/xesite/api.go76
1 files changed, 76 insertions, 0 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
+}