optimize code

This commit is contained in:
xing 2024-01-14 22:07:16 +08:00
parent 0352dd0fd0
commit ad9bdc7574
13 changed files with 80 additions and 48 deletions

View File

@ -10,6 +10,28 @@ import (
var tmp = "Mon, 02 Jan 2006 15:04:05 GMT"
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
}
}
func isCacheExpired(c *gin.Context, lastTime time.Time) bool {
eTag := str.Md5(lastTime.Format(tmp))
since := c.Request.Header.Get("If-Modified-Since")
@ -24,7 +46,7 @@ func isCacheExpired(c *gin.Context, lastTime time.Time) bool {
return true
}
func Feed(c *gin.Context) {
func SiteFeed(c *gin.Context) {
feed := cache.FeedCache()
if !isCacheExpired(c, feed.GetLastSetTime(c)) {
c.Status(http.StatusNotModified)

View File

@ -1,7 +1,6 @@
package actions
import (
"github.com/fthvgb1/wp-go/app/pkg/constraints"
"github.com/fthvgb1/wp-go/app/theme"
"github.com/fthvgb1/wp-go/app/theme/wp"
"github.com/gin-gonic/gin"
@ -9,14 +8,8 @@ import (
func ThemeHook(scene string) func(*gin.Context) {
return func(c *gin.Context) {
s := scene
if scene == constraints.Home {
if _, ok := c.GetQuery("s"); ok {
s = constraints.Search
}
}
t := theme.GetCurrentTemplateName()
h := wp.NewHandle(c, s, t)
h := wp.NewHandle(c, scene, t)
h.Index = wp.NewIndexHandle(h)
h.Detail = wp.NewDetailHandle(h)
templ, _ := theme.GetTemplate(t)

View File

@ -64,7 +64,8 @@ func SetupRouter() *gin.Engine {
store := cookie.NewStore([]byte("secret"))
r.Use(sessions.Sessions("go-wp", store))
r.GET("/", middleware.SearchLimit(c.SingleIpSearchNum), actions.ThemeHook(constraints.Home))
r.GET("/", actions.Feed, middleware.SearchLimit(c.SingleIpSearchNum),
actions.ThemeHook(constraints.Home))
r.GET("/page/:page", actions.ThemeHook(constraints.Home))
r.GET("/p/category/:category", actions.ThemeHook(constraints.Category))
r.GET("/p/category/:category/page/:page", actions.ThemeHook(constraints.Category))
@ -78,7 +79,7 @@ func SetupRouter() *gin.Engine {
r.GET("/p/:id", actions.ThemeHook(constraints.Detail))
r.GET("/p/:id/comment-page-:page", actions.ThemeHook(constraints.Detail))
r.GET("/p/:id/feed", actions.PostFeed)
r.GET("/feed", actions.Feed)
r.GET("/feed", actions.SiteFeed)
r.GET("/comments/feed", actions.CommentsFeed)
//r.NoRoute(actions.ThemeHook(constraints.NoRoute))
commentMiddleWare, _ := middleware.FlowLimit(c.MaxRequestSleepNum, 5, c.CacheTime.SleepTime)

View File

@ -85,11 +85,11 @@ func InitActionsCommonCache() {
return config.GetConfig().CacheTime.UserInfoCacheTime
})
cachemanager.NewVarMemoryCache(feed, time.Hour, "feed")
cachemanager.NewVarMemoryCache(SiteFeed, time.Hour, "siteFeed")
cachemanager.NewMemoryMapCache(nil, PostFeed, time.Hour, "postFeed")
cachemanager.NewVarMemoryCache(commentsFeed, time.Hour, "commentsFeed")
cachemanager.NewVarMemoryCache(CommentsFeed, time.Hour, "commentsFeed")
cachemanager.NewMemoryMapCache[string, string](nil, nil, 15*time.Minute, "NewComment")

View File

@ -35,13 +35,15 @@ func InitFeed() {
}
}
// CommentsFeedCache query func see CommentsFeed
func CommentsFeedCache() *cache.VarCache[[]string] {
r, _ := cachemanager.GetVarCache[[]string]("commentsFeed")
return r
}
// FeedCache query func see SiteFeed
func FeedCache() *cache.VarCache[[]string] {
r, _ := cachemanager.GetVarCache[[]string]("feed")
r, _ := cachemanager.GetVarCache[[]string]("siteFeed")
return r
}
@ -51,7 +53,7 @@ func PostFeedCache() *cache.MapCache[string, string] {
return r
}
func feed(c context.Context, _ ...any) (xml []string, err error) {
func SiteFeed(c context.Context, _ ...any) (xml []string, err error) {
r := RecentPosts(c, 10)
ids := slice.Map(r, func(t models.Posts) uint64 {
return t.Id
@ -167,7 +169,7 @@ func PostFeed(c context.Context, id string, _ ...any) (x string, err error) {
return
}
func commentsFeed(c context.Context, _ ...any) (r []string, err error) {
func CommentsFeed(c context.Context, _ ...any) (r []string, err error) {
commens := RecentComments(c, 10)
rs := templateRss
rs.Title = fmt.Sprintf("\"%s\"的评论", wpconfig.GetOption("blogname"))

View File

@ -31,11 +31,7 @@
{{end}}
</div>
<footer id="colophon" class="site-footer">
<div class="site-info">
<a href="https://cn.wordpress.org/" class="imprint">自豪地采用WordPress</a>
</div>
</footer>
{{template "common/colophon" .}}
</div>
{{template "layout/footer" .}}

View File

@ -1,9 +0,0 @@
{{define "layout/colophon"}}
<footer id="colophon" class="site-footer">
<div class="wrap">
<div class="site-info">
<a href="https://cn.wordpress.org/" class="imprint">自豪地采用WordPress</a>
</div>
</div>
</footer>
{{end}}

View File

@ -132,7 +132,7 @@
</div>
</div>
{{template "layout/colophon"}}
{{template "common/colophon" .}}
</div>

View File

@ -77,6 +77,6 @@
{{end}}
</div>
{{template "layout/colophon"}}
{{template "common/colophon" .}}
</div>
{{end}}

View File

@ -66,6 +66,7 @@ func configs(h *wp.Handle) {
wp.NewHandleFn(errorsHandle, 80.005, "errorsHandle"),
)
videoHeader(h)
h.SetData("colophon", colophon)
h.Detail.CommentRender = commentFormat
h.Detail.CommentPageEle = commentPageEle
h.CommonComponents()
@ -123,6 +124,14 @@ func (c comment) FormatLi(_ context.Context, m models.Comments, depth, maxDepth,
return plugins.FormatLi(commentLi, m, respondFn, depth, maxDepth, page, isTls, isThreadComments, eo, parent)
}
var colophon = `<footer id="colophon" class="site-footer">
<div class="wrap">
<div class="site-info">
<a href="https://github.com/fthvgb1/wp-go" class="imprint">自豪地采用 wp-go</a>
</div>
</div>
</footer>`
var respondStr = `<a rel="nofollow" class="comment-reply-link"
href="/p/{{PostId}}?replytocom={{CommentId}}#respond" data-commentid="{{CommentId}}" data-postid="{{PostId}}"
data-belowelement="div-comment-{{CommentId}}" data-respondelement="respond"

View File

@ -105,18 +105,16 @@ func NewIndexParams(ctx *gin.Context) *IndexParams {
func (i *IndexParams) ParseSearchs() {
s := i.Ctx.Query("s")
if s != "" {
q := str.Join("%", s, "%")
i.Where = append(i.Where, []string{
"and", "post_title", "like", q, "",
"or", "post_content", "like", q, "",
"or", "post_excerpt", "like", q, "",
}, []string{"post_password", ""})
i.PostType = append(i.PostType, "Page", "attachment")
i.Header = fmt.Sprintf("<span>%s</span>的搜索结果", s)
i.setTitleLR(str.Join(`"`, s, `"`, "的搜索结果"), i.BlogName)
i.Search = s
}
q := str.Join("%", s, "%")
i.Where = append(i.Where, []string{
"and", "post_title", "like", q, "",
"or", "post_content", "like", q, "",
"or", "post_excerpt", "like", q, "",
}, []string{"post_password", ""})
i.PostType = append(i.PostType, "Page", "attachment")
i.Header = fmt.Sprintf("<span>%s</span>的搜索结果", s)
i.setTitleLR(str.Join(`"`, s, `"`, "的搜索结果"), i.BlogName)
i.Search = s
}
func (i *IndexParams) ParseArchives() error {
year := i.Ctx.Param("year")

View File

@ -23,6 +23,10 @@ var plainRouteParam = reload.Vars([]Plain{
},
Scene: constraints.Detail,
},
{
Action: "s",
Scene: constraints.Search,
},
{
Scene: constraints.Category,
Fn: func(h *wp.Handle) bool {
@ -86,6 +90,12 @@ var plainRouteParam = reload.Vars([]Plain{
},
})
func SetExplainRouteParam(p []Plain) {
plainRouteParam.Store(p)
}
func GetExplainRouteParam() []Plain {
return plainRouteParam.Load()
}
func PushExplainRouteParam(explain ...Plain) {
v := plainRouteParam.Load()
v = append(v, explain...)
@ -108,7 +118,6 @@ func MixWithPlain(h *wp.Handle) {
if !explain.Fn(h) {
continue
}
h.C.Set("inited", false)
if explain.Scene != "" {
h.SetScene(explain.Scene)
}
@ -119,15 +128,14 @@ func MixWithPlain(h *wp.Handle) {
if explain.Scene == "" {
continue
}
action := h.C.Query(explain.Action)
if action == "" {
q := h.C.Query(explain.Action)
if q == "" {
continue
}
h.SetScene(explain.Scene)
for query, param := range explain.Param {
h.C.AddParam(param, h.C.Query(query))
}
h.C.Set("inited", false)
wp.Run(h, nil)
h.Abort()
return

View File

@ -23,4 +23,16 @@
{{if .sidebarsWidgets}}
{{.sidebarsWidgets|unescaped}}
{{end}}
{{end}}
{{define "common/colophon"}}
{{if .colophon}}
{{.colophon|unescaped}}
{{else}}
<footer id="colophon" class="site-footer">
<div class="site-info">
<a href="https://github.com/fthvgb1/wp-go" class="imprint">自豪地采用 wp-go</a>
</div>
</footer>
{{end}}
{{end}}