wp-go/app/actions/feed.go

107 lines
2.2 KiB
Go
Raw Normal View History

2022-10-06 13:33:04 +00:00
package actions
import (
2023-05-04 12:36:17 +00:00
"github.com/fthvgb1/wp-go/app/pkg/cache"
2023-01-21 11:31:23 +00:00
str "github.com/fthvgb1/wp-go/helper/strings"
2022-10-06 13:33:04 +00:00
"github.com/gin-gonic/gin"
"net/http"
"time"
)
2022-10-07 12:37:42 +00:00
var tmp = "Mon, 02 Jan 2006 15:04:05 GMT"
2022-10-07 15:16:25 +00:00
2024-01-14 14:07:16 +00:00
func Feed(c *gin.Context) {
v, ok := c.GetQuery("feed")
if !ok || v == "" {
c.Next()
return
}
switch v {
case "rss2":
p, ok := c.GetQuery("p")
if ok && p != "" {
c.AddParam("id", p)
PostFeed(c)
} else {
SiteFeed(c)
}
return
case "comments-rss2":
CommentsFeed(c)
return
}
}
2022-10-07 12:37:42 +00:00
func isCacheExpired(c *gin.Context, lastTime time.Time) bool {
2023-01-21 11:31:23 +00:00
eTag := str.Md5(lastTime.Format(tmp))
2022-10-07 12:37:42 +00:00
since := c.Request.Header.Get("If-Modified-Since")
cTag := c.Request.Header.Get("If-None-Match")
if since != "" && cTag != "" {
cGMT, err := time.Parse(tmp, since)
if err == nil && lastTime.Unix() <= cGMT.Unix() && eTag == cTag {
c.Status(http.StatusNotModified)
return false
2022-10-06 13:33:04 +00:00
}
2022-10-07 12:37:42 +00:00
}
return true
}
2024-01-14 14:07:16 +00:00
func SiteFeed(c *gin.Context) {
2023-11-02 14:40:13 +00:00
feed := cache.FeedCache()
if !isCacheExpired(c, feed.GetLastSetTime(c)) {
2022-10-07 14:27:34 +00:00
c.Status(http.StatusNotModified)
return
}
2023-11-02 14:40:13 +00:00
r, err := feed.GetCache(c, time.Second, c)
if err != nil {
c.Status(http.StatusInternalServerError)
c.Abort()
c.Error(err)
return
2022-10-07 12:37:42 +00:00
}
2023-11-02 14:40:13 +00:00
setFeed(r[0], c, feed.GetLastSetTime(c))
2022-10-06 13:33:04 +00:00
}
2022-10-07 14:27:34 +00:00
2022-10-07 14:54:53 +00:00
func setFeed(s string, c *gin.Context, t time.Time) {
lastTimeGMT := t.Format(tmp)
2023-01-21 11:31:23 +00:00
eTag := str.Md5(lastTimeGMT)
2022-10-07 14:27:34 +00:00
c.Header("Content-Type", "application/rss+xml; charset=UTF-8")
c.Header("Last-Modified", lastTimeGMT)
c.Header("ETag", eTag)
2022-10-07 14:54:53 +00:00
c.String(http.StatusOK, s)
2022-10-07 14:27:34 +00:00
}
func PostFeed(c *gin.Context) {
id := c.Param("id")
2023-11-02 14:40:13 +00:00
postFeed := cache.PostFeedCache()
if !isCacheExpired(c, postFeed.GetLastSetTime(c, id)) {
2022-10-07 14:27:34 +00:00
c.Status(http.StatusNotModified)
return
2022-10-07 14:27:34 +00:00
}
2023-11-25 09:49:20 +00:00
s, err := postFeed.GetCache(c, id, time.Second)
if err != nil {
c.Status(http.StatusInternalServerError)
c.Abort()
c.Error(err)
return
}
2023-11-02 14:40:13 +00:00
setFeed(s, c, postFeed.GetLastSetTime(c, id))
2022-10-07 14:27:34 +00:00
}
2022-10-08 06:01:05 +00:00
func CommentsFeed(c *gin.Context) {
2023-11-02 14:40:13 +00:00
feed := cache.CommentsFeedCache()
if !isCacheExpired(c, feed.GetLastSetTime(c)) {
2022-10-08 06:01:05 +00:00
c.Status(http.StatusNotModified)
return
}
2023-11-02 14:40:13 +00:00
r, err := feed.GetCache(c, time.Second, c)
if err != nil {
c.Status(http.StatusInternalServerError)
c.Abort()
c.Error(err)
return
2022-10-08 06:01:05 +00:00
}
2023-11-02 14:40:13 +00:00
setFeed(r[0], c, feed.GetLastSetTime(c))
2022-10-08 06:01:05 +00:00
}