diff --git a/app/actions/feed.go b/app/actions/feed.go index 470b003..64169a5 100644 --- a/app/actions/feed.go +++ b/app/actions/feed.go @@ -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) diff --git a/app/actions/themehook.go b/app/actions/themehook.go index e11bfdb..083c1a5 100644 --- a/app/actions/themehook.go +++ b/app/actions/themehook.go @@ -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) diff --git a/app/cmd/route/route.go b/app/cmd/route/route.go index d3a05ec..9c2af38 100644 --- a/app/cmd/route/route.go +++ b/app/cmd/route/route.go @@ -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) diff --git a/app/pkg/cache/cache.go b/app/pkg/cache/cache.go index 56c22ae..891ddc8 100644 --- a/app/pkg/cache/cache.go +++ b/app/pkg/cache/cache.go @@ -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") diff --git a/app/pkg/cache/feed.go b/app/pkg/cache/feed.go index a433564..fab6bab 100644 --- a/app/pkg/cache/feed.go +++ b/app/pkg/cache/feed.go @@ -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")) diff --git a/app/theme/twentyfifteen/layout/base.gohtml b/app/theme/twentyfifteen/layout/base.gohtml index 40e3341..3007596 100644 --- a/app/theme/twentyfifteen/layout/base.gohtml +++ b/app/theme/twentyfifteen/layout/base.gohtml @@ -31,11 +31,7 @@ {{end}} - + {{template "common/colophon" .}} {{template "layout/footer" .}} diff --git a/app/theme/twentyseventeen/layout/colophon.gohtml b/app/theme/twentyseventeen/layout/colophon.gohtml deleted file mode 100644 index efb3fa9..0000000 --- a/app/theme/twentyseventeen/layout/colophon.gohtml +++ /dev/null @@ -1,9 +0,0 @@ -{{define "layout/colophon"}} - -{{end}} \ No newline at end of file diff --git a/app/theme/twentyseventeen/posts/detail.gohtml b/app/theme/twentyseventeen/posts/detail.gohtml index 8dfa0cf..b7fc41a 100644 --- a/app/theme/twentyseventeen/posts/detail.gohtml +++ b/app/theme/twentyseventeen/posts/detail.gohtml @@ -132,7 +132,7 @@ - {{template "layout/colophon"}} + {{template "common/colophon" .}} diff --git a/app/theme/twentyseventeen/posts/index.gohtml b/app/theme/twentyseventeen/posts/index.gohtml index 80c6388..7da52c7 100644 --- a/app/theme/twentyseventeen/posts/index.gohtml +++ b/app/theme/twentyseventeen/posts/index.gohtml @@ -77,6 +77,6 @@ {{end}} - {{template "layout/colophon"}} + {{template "common/colophon" .}} {{end}} \ No newline at end of file diff --git a/app/theme/twentyseventeen/twentyseventeen.go b/app/theme/twentyseventeen/twentyseventeen.go index 1e24812..33d76fc 100644 --- a/app/theme/twentyseventeen/twentyseventeen.go +++ b/app/theme/twentyseventeen/twentyseventeen.go @@ -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 = `` + var respondStr = `%s的搜索结果", 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("%s的搜索结果", s) + i.setTitleLR(str.Join(`"`, s, `"`, "的搜索结果"), i.BlogName) + i.Search = s } func (i *IndexParams) ParseArchives() error { year := i.Ctx.Param("year") diff --git a/app/theme/wp/middleware/middleware.go b/app/theme/wp/middleware/middleware.go index 6bc2631..8fac73c 100644 --- a/app/theme/wp/middleware/middleware.go +++ b/app/theme/wp/middleware/middleware.go @@ -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 diff --git a/app/theme/wp/template.gohtml b/app/theme/wp/template.gohtml index 9f0cc1b..eebb3ae 100644 --- a/app/theme/wp/template.gohtml +++ b/app/theme/wp/template.gohtml @@ -23,4 +23,16 @@ {{if .sidebarsWidgets}} {{.sidebarsWidgets|unescaped}} {{end}} +{{end}} + +{{define "common/colophon"}} + {{if .colophon}} + {{.colophon|unescaped}} + {{else}} + + {{end}} {{end}} \ No newline at end of file