gopherss/internal/server/main.go

79 lines
1.6 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)
2021-02-21 10:29:07 +00:00
app.Delete("/api/feed/:id", api.DeleteFeed)
2020-10-17 13:30:30 +00:00
app.Get("/api/item/:id", api.GetItem)
2020-11-08 20:19:41 +00:00
app.Post("/api/item/:id/save", api.SaveItem)
2020-10-17 13:30:30 +00:00
app.Get("/api/unread", api.GetUnread)
2020-11-08 20:19:41 +00:00
app.Get("/api/saved", api.GetSaved)
2020-11-08 19:38:55 +00:00
app.Get("/api/all", api.GetAll)
2020-10-17 13:30:30 +00:00
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
}