Initial commit
This commit is contained in:
31
internal/server/api.go
Normal file
31
internal/server/api.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/averagemarcus/gopherss/internal/feeds"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type API struct {
|
||||
FeedStore *feeds.FeedStore
|
||||
}
|
||||
|
||||
func (a *API) GetFeeds(c *fiber.Ctx) error {
|
||||
return c.JSON(a.FeedStore.GetFeeds())
|
||||
}
|
||||
|
||||
func (a *API) GetFeed(c *fiber.Ctx) error {
|
||||
return c.JSON(a.FeedStore.GetFeed(c.Params("id")))
|
||||
}
|
||||
|
||||
func (a *API) GetItem(c *fiber.Ctx) error {
|
||||
return c.JSON(a.FeedStore.GetItem(c.Params("id")))
|
||||
}
|
||||
|
||||
func (a *API) GetUnread(c *fiber.Ctx) error {
|
||||
return c.JSON(a.FeedStore.GetUnread())
|
||||
}
|
||||
|
||||
func (a *API) PostRead(c *fiber.Ctx) error {
|
||||
a.FeedStore.MarkAsRead(c.Params("id"))
|
||||
return nil
|
||||
}
|
80
internal/server/main.go
Normal file
80
internal/server/main.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"time"
|
||||
|
||||
"github.com/dustin/go-humanize"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/template/html"
|
||||
|
||||
"github.com/averagemarcus/gopherss/internal/feeds"
|
||||
)
|
||||
|
||||
var api API
|
||||
|
||||
func init() {
|
||||
api = API{
|
||||
FeedStore: &feeds.FeedStore{},
|
||||
}
|
||||
}
|
||||
|
||||
func Start(port string) error {
|
||||
engine := html.New("./views", ".html")
|
||||
engine.Reload(true)
|
||||
|
||||
engine.AddFunc("htmlSafe", func(html string) template.HTML {
|
||||
return template.HTML(html)
|
||||
})
|
||||
engine.AddFunc("humanDate", func(date time.Time) template.HTML {
|
||||
return template.HTML(humanize.Time(date))
|
||||
})
|
||||
engine.AddFunc("coalesce", func(args ...*string) string {
|
||||
for _, s := range args {
|
||||
if s != nil && *s != "" {
|
||||
return *s
|
||||
}
|
||||
}
|
||||
return ""
|
||||
})
|
||||
|
||||
app := fiber.New(fiber.Config{
|
||||
Views: engine,
|
||||
})
|
||||
|
||||
app.Static("/", "./views/static")
|
||||
app.Get("/", GetFeeds)
|
||||
app.Post("/opml", PostOPML)
|
||||
|
||||
// API
|
||||
app.Get("/api/feeds", api.GetFeeds)
|
||||
// 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)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
for _, outline := range opml.Outlines {
|
||||
feeds.RefreshFeed(outline.XmlUrl)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user