From ec2aa30eb6f69933046ef2d339d738b5fb8a19d1 Mon Sep 17 00:00:00 2001 From: xing Date: Tue, 8 Nov 2022 17:41:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=B7=BB=E5=8A=A0=E8=AF=84?= =?UTF-8?q?=E8=AE=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- actions/comment.go | 42 +++++++++++++++++++++++++++++------------- actions/detail.go | 10 ++++++++++ actions/feed.go | 13 +++++++------ cache/map.go | 8 ++++---- cache/slice.go | 3 +-- 5 files changed, 51 insertions(+), 25 deletions(-) diff --git a/actions/comment.go b/actions/comment.go index 9b228ce..44fb59f 100644 --- a/actions/comment.go +++ b/actions/comment.go @@ -6,23 +6,27 @@ import ( "fmt" "github.com/gin-gonic/gin" "github/fthvgb1/wp-go/actions/common" + "github/fthvgb1/wp-go/cache" "github/fthvgb1/wp-go/config" "github/fthvgb1/wp-go/logs" "github/fthvgb1/wp-go/mail" "github/fthvgb1/wp-go/models/wp" "io" "net/http" - "net/http/cookiejar" + "net/url" "strconv" "strings" "time" ) +var commentCache = cache.NewMapCacheByFn[string, string](nil, 15*time.Minute) + func PostComment(c *gin.Context) { - jar, _ := cookiejar.New(nil) cli := &http.Client{ - Jar: jar, Timeout: time.Second * 3, + CheckRedirect: func(req *http.Request, via []*http.Request) error { + return http.ErrUseLastResponse + }, } data, err := c.GetRawData() defer func() { @@ -44,18 +48,28 @@ func PostComment(c *gin.Context) { return } defer req.Body.Close() - for k, v := range c.Request.Header { - req.Header.Set(k, v[0]) - } + req.Header = c.Request.Header.Clone() res, err := cli.Do(req) - if err != nil { + if err != nil && err != http.ErrUseLastResponse { return } - if res.Request.Response != nil && res.Request.Response.StatusCode == http.StatusFound { - for _, cookie := range res.Request.Response.Cookies() { - c.SetCookie(cookie.Name, cookie.Value, cookie.MaxAge, cookie.Path, cookie.Domain, cookie.Secure, cookie.HttpOnly) + if res.StatusCode == http.StatusFound { + u := res.Header.Get("Location") + up, err := url.Parse(u) + if err != nil { + return + } + cu, err := url.Parse(config.Conf.PostCommentUrl) + if err != nil { + return + } + up.Host = cu.Host + + ress, err := http.DefaultClient.Get(up.String()) + + if err != nil { + return } - //c.Redirect(http.StatusFound, res.Request.Response.Header.Get("Location")) cc := c.Copy() go func() { id, err := strconv.ParseUint(i, 10, 64) @@ -72,11 +86,13 @@ func PostComment(c *gin.Context) { err = mail.SendMail([]string{config.Conf.Mail.User}, su, comment) logs.ErrPrintln(err, "发送邮件", config.Conf.Mail.User, su, comment) }() - s, err := io.ReadAll(res.Body) + + s, err := io.ReadAll(ress.Body) if err != nil { return } - c.String(http.StatusOK, string(s)) + commentCache.Set(up.RawQuery, string(s)) + c.Redirect(http.StatusFound, res.Header.Get("Location")) return } s, err := io.ReadAll(res.Body) diff --git a/actions/detail.go b/actions/detail.go index a1ac19e..38649ab 100644 --- a/actions/detail.go +++ b/actions/detail.go @@ -39,12 +39,16 @@ func Detail(c *gin.Context) { "categories": categoryItems, "recentComments": recentComments, } + isApproveComment := false defer func() { status := http.StatusOK if err != nil { status = http.StatusInternalServerError c.Error(err) } + if isApproveComment == true { + return + } c.HTML(status, "twentyfifteen/posts/detail.gohtml", h) }() id := c.Param("id") @@ -74,6 +78,12 @@ func Detail(c *gin.Context) { if post.PostPassword != "" && pw != post.PostPassword { common.PasswdProjectContent(&post) showComment = false + } else if s, ok := commentCache.Get(c.Request.URL.RawQuery); ok && s != "" && (post.PostPassword == "" || post.PostPassword != "" && pw == post.PostPassword) { + c.Writer.WriteHeader(http.StatusOK) + c.Writer.Header().Set("Content-Type", "text/html; charset=utf-8") + _, err = c.Writer.WriteString(s) + isApproveComment = true + return } plugins.ApplyPlugin(plugins.NewPostPlugin(c, plugins.Detail), &post) comments, err := common.PostComments(c, post.Id) diff --git a/actions/feed.go b/actions/feed.go index d1e8c21..286277c 100644 --- a/actions/feed.go +++ b/actions/feed.go @@ -38,6 +38,7 @@ func InitFeed() { func ClearCache() { postFeedCache.ClearExpired() + commentCache.ClearExpired() } func isCacheExpired(c *gin.Context, lastTime time.Time) bool { @@ -55,7 +56,7 @@ func isCacheExpired(c *gin.Context, lastTime time.Time) bool { } func Feed(c *gin.Context) { - if !isCacheExpired(c, feedCache.SetTime()) { + if !isCacheExpired(c, feedCache.GetLastSetTime()) { c.Status(http.StatusNotModified) } else { r, err := feedCache.GetCache(c, time.Second, c) @@ -65,7 +66,7 @@ func Feed(c *gin.Context) { c.Error(err) return } - setFeed(r[0], c, feedCache.SetTime()) + setFeed(r[0], c, feedCache.GetLastSetTime()) } } @@ -126,7 +127,7 @@ func setFeed(s string, c *gin.Context, t time.Time) { func PostFeed(c *gin.Context) { id := c.Param("id") - if !isCacheExpired(c, postFeedCache.GetSetTime(id)) { + if !isCacheExpired(c, postFeedCache.GetLastSetTime(id)) { c.Status(http.StatusNotModified) } else { s, err := postFeedCache.GetCache(c, id, time.Second, c, id) @@ -136,7 +137,7 @@ func PostFeed(c *gin.Context) { c.Error(err) return } - setFeed(s, c, postFeedCache.GetSetTime(id)) + setFeed(s, c, postFeedCache.GetLastSetTime(id)) } } @@ -205,7 +206,7 @@ func postFeed(arg ...any) (x string, err error) { } func CommentsFeed(c *gin.Context) { - if !isCacheExpired(c, commentsFeedCache.SetTime()) { + if !isCacheExpired(c, commentsFeedCache.GetLastSetTime()) { c.Status(http.StatusNotModified) } else { r, err := commentsFeedCache.GetCache(c, time.Second, c) @@ -215,7 +216,7 @@ func CommentsFeed(c *gin.Context) { c.Error(err) return } - setFeed(r[0], c, commentsFeedCache.SetTime()) + setFeed(r[0], c, commentsFeedCache.GetLastSetTime()) } } diff --git a/cache/map.go b/cache/map.go index 4da382c..213beba 100644 --- a/cache/map.go +++ b/cache/map.go @@ -31,7 +31,7 @@ func (m *MapCache[K, V]) SetCacheFunc(fn func(...any) (V, error)) { m.cacheFunc = fn } -func (m *MapCache[K, V]) GetSetTime(k K) (t time.Time) { +func (m *MapCache[K, V]) GetLastSetTime(k K) (t time.Time) { r, ok := m.data.Load(k) if ok { t = r.setTime @@ -93,13 +93,13 @@ func (m *MapCache[K, V]) Flush() { m.data = safety.NewMap[K, mapCacheStruct[V]]() } -func (m *MapCache[K, V]) Get(k K) V { +func (m *MapCache[K, V]) Get(k K) (V, bool) { r, ok := m.data.Load(k) if ok { - return r.data + return r.data, ok } var rr V - return rr + return rr, false } func (m *MapCache[K, V]) Set(k K, v V) { diff --git a/cache/slice.go b/cache/slice.go index 8e9d163..e4602d1 100644 --- a/cache/slice.go +++ b/cache/slice.go @@ -22,8 +22,7 @@ type slice[T any] struct { incr int } -func (c *SliceCache[T]) SetTime() time.Time { - +func (c *SliceCache[T]) GetLastSetTime() time.Time { return c.v.Load().setTime }