gopherss/internal/server/main.go

75 lines
1.5 KiB
Go
Raw Normal View History

2020-10-17 13:30:30 +00:00
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,
})
2020-10-17 18:02:52 +00:00
app.Static("/", "./views")
2020-10-17 13:30:30 +00:00
app.Post("/opml", PostOPML)
// API
app.Get("/api/feeds", api.GetFeeds)
2020-10-17 18:02:52 +00:00
app.Post("/api/feeds", api.PostFeed)
2020-10-17 13:30:30 +00:00
app.Get("/api/feed/:id", api.GetFeed)
app.Get("/api/item/:id", api.GetItem)
app.Get("/api/unread", api.GetUnread)
app.Post("/api/read/:id", api.PostRead)
2020-10-17 18:02:52 +00:00
app.Post("/api/read", api.PostReadAll)
app.Get("/api/refresh", api.RefreshAll)
2020-10-17 13:30:30 +00:00
return app.Listen(fmt.Sprintf(":%s", port))
}
func PostOPML(c *fiber.Ctx) error {
opml := &feeds.Opml{}
if err := c.BodyParser(opml); err != nil {
return err
}
2020-10-17 18:02:52 +00:00
f := []feeds.Feed{}
2020-10-17 13:30:30 +00:00
for _, outline := range opml.Outlines {
2020-10-17 18:02:52 +00:00
f = append(f, feeds.RefreshFeed(outline.XmlUrl))
2020-10-17 13:30:30 +00:00
}
2020-10-17 18:02:52 +00:00
return c.JSON(f)
2020-10-17 13:30:30 +00:00
}