2022-10-06 13:33:04 +00:00
|
|
|
package actions
|
|
|
|
|
|
|
|
import (
|
2023-01-21 11:31:23 +00:00
|
|
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
2023-01-19 13:02:39 +00:00
|
|
|
"github.com/fthvgb1/wp-go/internal/pkg/cache"
|
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
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func Feed(c *gin.Context) {
|
2023-01-19 13:02:39 +00:00
|
|
|
if !isCacheExpired(c, cache.FeedCache().GetLastSetTime()) {
|
2022-10-07 14:27:34 +00:00
|
|
|
c.Status(http.StatusNotModified)
|
|
|
|
} else {
|
2023-01-19 13:02:39 +00:00
|
|
|
r, err := cache.FeedCache().GetCache(c, time.Second, c)
|
2022-10-07 14:54:53 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Status(http.StatusInternalServerError)
|
|
|
|
c.Abort()
|
|
|
|
c.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2023-01-19 13:02:39 +00:00
|
|
|
setFeed(r[0], c, cache.FeedCache().GetLastSetTime())
|
2022-10-07 12:37:42 +00:00
|
|
|
}
|
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-01-19 13:02:39 +00:00
|
|
|
if !isCacheExpired(c, cache.PostFeedCache().GetLastSetTime(id)) {
|
2022-10-07 14:27:34 +00:00
|
|
|
c.Status(http.StatusNotModified)
|
|
|
|
} else {
|
2023-01-19 13:02:39 +00:00
|
|
|
s, err := cache.PostFeedCache().GetCache(c, id, time.Second, c, id)
|
2022-10-07 14:27:34 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Status(http.StatusInternalServerError)
|
|
|
|
c.Abort()
|
|
|
|
c.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2023-01-19 13:02:39 +00:00
|
|
|
setFeed(s, c, cache.PostFeedCache().GetLastSetTime(id))
|
2022-10-07 14:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-08 06:01:05 +00:00
|
|
|
|
|
|
|
func CommentsFeed(c *gin.Context) {
|
2023-01-19 13:02:39 +00:00
|
|
|
if !isCacheExpired(c, cache.CommentsFeedCache().GetLastSetTime()) {
|
2022-10-08 06:01:05 +00:00
|
|
|
c.Status(http.StatusNotModified)
|
|
|
|
} else {
|
2023-01-19 13:02:39 +00:00
|
|
|
r, err := cache.CommentsFeedCache().GetCache(c, time.Second, c)
|
2022-10-08 06:01:05 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Status(http.StatusInternalServerError)
|
|
|
|
c.Abort()
|
|
|
|
c.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2023-01-19 13:02:39 +00:00
|
|
|
setFeed(r[0], c, cache.CommentsFeedCache().GetLastSetTime())
|
2022-10-08 06:01:05 +00:00
|
|
|
}
|
|
|
|
}
|