From 25a389d60de4ade1b15df741360269b363551ae3 Mon Sep 17 00:00:00 2001 From: Marcus Noble Date: Sat, 17 Oct 2020 19:02:52 +0100 Subject: [PATCH] Updated to use Vue for frontend --- internal/feeds/refresh.go | 7 +- internal/feeds/store.go | 2 +- internal/server/api.go | 27 +++ internal/server/main.go | 20 +- views/index.html | 401 ++++++++++++++++++++++++++++---------- views/layouts/main.html | 103 ---------- views/static/feed-item.js | 8 +- views/static/style.css | 116 +++++++++++ 8 files changed, 459 insertions(+), 225 deletions(-) delete mode 100644 views/layouts/main.html create mode 100644 views/static/style.css diff --git a/internal/feeds/refresh.go b/internal/feeds/refresh.go index 727cd32..4865c97 100644 --- a/internal/feeds/refresh.go +++ b/internal/feeds/refresh.go @@ -28,8 +28,9 @@ func Refresh() error { } } -func RefreshFeed(url string) error { +func RefreshFeed(url string) Feed { fmt.Printf("Refreshing %s\n", url) + var feed Feed f, err := fp.ParseURL(url) if err != nil { fmt.Printf("Failed to refresh %s\n", url) @@ -39,7 +40,7 @@ func RefreshFeed(url string) error { imageURL = f.Image.URL } - feed := Feed{ + feed = Feed{ ID: strings.ReplaceAll(base64.StdEncoding.EncodeToString([]byte(url)), "/", ""), Title: f.Title, Description: f.Description, @@ -73,5 +74,5 @@ func RefreshFeed(url string) error { fmt.Printf("Finished refreshing '%s'\n", feed.Title) } - return nil + return feed } diff --git a/internal/feeds/store.go b/internal/feeds/store.go index 5f1090c..36b80e3 100644 --- a/internal/feeds/store.go +++ b/internal/feeds/store.go @@ -32,7 +32,7 @@ func (fs *FeedStore) GetItem(id string) *Item { func (fs *FeedStore) GetFeeds() *[]Feed { feeds := &[]Feed{} - fs.getDB().Preload("Items").Order("title asc").Find(feeds) + fs.getDB().Order("title asc").Find(feeds) return feeds } diff --git a/internal/server/api.go b/internal/server/api.go index f25fbde..9c85ce0 100644 --- a/internal/server/api.go +++ b/internal/server/api.go @@ -17,6 +17,14 @@ func (a *API) GetFeed(c *fiber.Ctx) error { return c.JSON(a.FeedStore.GetFeed(c.Params("id"))) } +func (a *API) PostFeed(c *fiber.Ctx) error { + url := "" + if err := c.BodyParser(&url); err != nil { + return err + } + return c.JSON(feeds.RefreshFeed(url)) +} + func (a *API) GetItem(c *fiber.Ctx) error { return c.JSON(a.FeedStore.GetItem(c.Params("id"))) } @@ -29,3 +37,22 @@ func (a *API) PostRead(c *fiber.Ctx) error { a.FeedStore.MarkAsRead(c.Params("id")) return nil } + +func (a *API) PostReadAll(c *fiber.Ctx) error { + ids := &[]string{} + if err := c.BodyParser(ids); err != nil { + return err + } + for _, id := range *ids { + a.FeedStore.MarkAsRead(id) + } + return c.JSON(a.FeedStore.GetUnread()) +} + +func (a *API) RefreshAll(c *fiber.Ctx) error { + for _, feed := range *a.FeedStore.GetFeeds() { + feeds.RefreshFeed(feed.FeedURL) + } + + return c.JSON(a.FeedStore.GetUnread()) +} diff --git a/internal/server/main.go b/internal/server/main.go index 50ac9a7..02ff01e 100644 --- a/internal/server/main.go +++ b/internal/server/main.go @@ -43,38 +43,32 @@ func Start(port string) error { Views: engine, }) - app.Static("/", "./views/static") - app.Get("/", GetFeeds) + app.Static("/", "./views") app.Post("/opml", PostOPML) // API app.Get("/api/feeds", api.GetFeeds) - // app.Post("/api/feeds", api.PostFeed) + app.Post("/api/feeds", api.PostFeed) app.Get("/api/feed/:id", api.GetFeed) - // app.Get("/api/feed/:id/unread", api.GetFeedUnread) app.Get("/api/item/:id", api.GetItem) app.Get("/api/unread", api.GetUnread) app.Post("/api/read/:id", api.PostRead) + app.Post("/api/read", api.PostReadAll) + app.Get("/api/refresh", api.RefreshAll) return app.Listen(fmt.Sprintf(":%s", port)) } -func GetFeeds(c *fiber.Ctx) error { - return c.Render("index", fiber.Map{ - "Feeds": api.FeedStore.GetFeeds(), - "Unread": api.FeedStore.GetUnread(), - }, "layouts/main") -} - func PostOPML(c *fiber.Ctx) error { opml := &feeds.Opml{} if err := c.BodyParser(opml); err != nil { return err } + f := []feeds.Feed{} for _, outline := range opml.Outlines { - feeds.RefreshFeed(outline.XmlUrl) + f = append(f, feeds.RefreshFeed(outline.XmlUrl)) } - return nil + return c.JSON(f) } diff --git a/views/index.html b/views/index.html index d3c89d4..23a8a1b 100644 --- a/views/index.html +++ b/views/index.html @@ -1,108 +1,301 @@ + + + + + + Gopherss + + + + + + + + + + + + +
+

+ Gopherss +

+ +
+ + +
+
+ All + ({{items.length}}) +
+ +
+ {{feed.Title}} + ({{unreadCount(feed)}}) +
+ + + +
+
Add New Site
+
+
+
+ + +
Enter the direct URL to the feed
+
+
+ +
+ +
+ + +
+
+ +
+
+
+
+
+ +
+
+
+ {{item.FeedTitle}} + {{item.Created}} +

{{item.Title}}

+
+
+
+
+ +
+
+
+
+
+ No Items to Show +
+
+
-
-
-
All ({{len .Unread}})
- {{range .Feeds}} -
{{.Title}} ({{.UnreadCount}})
- {{end}} -
-
- {{range .Unread}} -
- {{.FeedTitle}} - {{humanDate .Created}} -

{{.Title}}

-
-
-
-
- -
+ +
- {{end}} -
-
- - - - - + + diff --git a/views/layouts/main.html b/views/layouts/main.html deleted file mode 100644 index 8440659..0000000 --- a/views/layouts/main.html +++ /dev/null @@ -1,103 +0,0 @@ - - - - - - Gopherss - - - - - - - - - - - - -
-

- Gopherss -

- {{embed}} -
- - diff --git a/views/static/feed-item.js b/views/static/feed-item.js index 631cfff..db880e6 100644 --- a/views/static/feed-item.js +++ b/views/static/feed-item.js @@ -9,11 +9,17 @@ class FeedItem extends HTMLElement { const template = document.createElement('template'); template.innerHTML = `