优化完善

This commit is contained in:
xing 2023-05-05 20:45:20 +08:00
parent 7c571654a6
commit b69b01f27b
8 changed files with 65 additions and 43 deletions

View File

@ -77,7 +77,7 @@ func SetupRouter() *gin.Engine {
r.GET("/p/:id/feed", actions.PostFeed) r.GET("/p/:id/feed", actions.PostFeed)
r.GET("/feed", actions.Feed) r.GET("/feed", actions.Feed)
r.GET("/comments/feed", actions.CommentsFeed) r.GET("/comments/feed", actions.CommentsFeed)
//r.NoRoute(actions.ThemeHook(constraints.NoRoute)) r.NoRoute(actions.ThemeHook(constraints.NoRoute))
commentMiddleWare, _ := middleware.FlowLimit(c.MaxRequestSleepNum, 5, c.CacheTime.SleepTime) commentMiddleWare, _ := middleware.FlowLimit(c.MaxRequestSleepNum, 5, c.CacheTime.SleepTime)
r.POST("/comment", commentMiddleWare, actions.PostComment) r.POST("/comment", commentMiddleWare, actions.PostComment)
if c.Pprof != "" { if c.Pprof != "" {

View File

@ -63,11 +63,11 @@ func configs(h *wp.Handle) {
components.WidgetArea(h) components.WidgetArea(h)
wp.ReplyCommentJs(h) wp.ReplyCommentJs(h)
h.SetData("customHeader", customHeader(h)) h.SetData("customHeader", customHeader(h))
h.PushRender(constraints.AllStats, wp.NewHandleFn(wp.IndexRender, 50, "wp.IndexRender")) wp.PushIndexHandler(constraints.PipeRender, h, wp.NewHandleFn(wp.IndexRender, 50, "wp.IndexRender"))
h.PushRender(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 50, "wp.DetailRender")) h.PushRender(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 50, "wp.DetailRender"))
h.PushRender(constraints.Detail, wp.NewHandleFn(postThumb, 60, "postThumb")) h.PushRender(constraints.Detail, wp.NewHandleFn(postThumb, 60, "postThumb"))
h.PushDataHandler(constraints.Detail, wp.NewHandleFn(wp.Details, 100, "wp.Details")) h.PushDataHandler(constraints.Detail, wp.NewHandleFn(wp.Detail, 100, "wp.Detail"))
h.PushDataHandler(constraints.AllScene, wp.NewHandleFn(wp.Indexs, 100, "wp.Indexs")) wp.PushIndexHandler(constraints.PipeData, h, wp.NewHandleFn(wp.Index, 100, "wp.Index"))
h.PushDataHandler(constraints.AllScene, wp.NewHandleFn(wp.PreCodeAndStats, 80, "wp.PreCodeAndStats")) h.PushDataHandler(constraints.AllScene, wp.NewHandleFn(wp.PreCodeAndStats, 80, "wp.PreCodeAndStats"))
h.PushRender(constraints.AllScene, wp.NewHandleFn(wp.PreTemplate, 70, "wp.PreTemplate")) h.PushRender(constraints.AllScene, wp.NewHandleFn(wp.PreTemplate, 70, "wp.PreTemplate"))
} }

View File

@ -71,10 +71,10 @@ func configs(h *wp.Handle) {
wp.ReplyCommentJs(h) wp.ReplyCommentJs(h)
h.Index.SetListPlugin(wp.PostsPlugins(wp.PostPlugin(postThumbnail), wp.GetListPostPlugins(conf.ListPagePlugins, wp.ListPostPlugins())...)) h.Index.SetListPlugin(wp.PostsPlugins(wp.PostPlugin(postThumbnail), wp.GetListPostPlugins(conf.ListPagePlugins, wp.ListPostPlugins())...))
wp.SetComponentsArgsForMap(h, widgets.Search, "{$form}", searchForm) wp.SetComponentsArgsForMap(h, widgets.Search, "{$form}", searchForm)
h.PushRender(constraints.AllStats, wp.NewHandleFn(wp.IndexRender, 10, "wp.IndexRender")) wp.PushIndexHandler(constraints.PipeRender, h, wp.NewHandleFn(wp.IndexRender, 10, "wp.IndexRender"))
h.PushRender(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 10, "wp.DetailRender")) h.PushRender(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 10, "wp.DetailRender"))
h.PushDataHandler(constraints.Detail, wp.NewHandleFn(detail, 100, "detail")) h.PushDataHandler(constraints.Detail, wp.NewHandleFn(wp.Detail, 100, "wp.Detail"), wp.NewHandleFn(postThumb, 90, "{theme}.postThumb"))
h.PushDataHandler(constraints.AllScene, wp.NewHandleFn(index, 100, "index")) wp.PushIndexHandler(constraints.PipeData, h, wp.NewHandleFn(wp.Index, 100, "wp.Index"))
h.PushDataHandler(constraints.AllScene, wp.NewHandleFn(wp.PreCodeAndStats, 90, "wp.PreCodeAndStats")) h.PushDataHandler(constraints.AllScene, wp.NewHandleFn(wp.PreCodeAndStats, 90, "wp.PreCodeAndStats"))
} }
@ -97,24 +97,8 @@ func errorsHandle(h *wp.Handle) {
} }
} }
func index(h *wp.Handle) { func postThumb(h *wp.Handle) {
if h.Scene() == constraints.Detail {
return
}
i := h.Index
err := i.BuildIndexData(wp.NewIndexParams(i.C))
if err != nil {
i.SetErr(err)
}
h.SetData("scene", h.Scene())
}
func detail(h *wp.Handle) {
d := h.Detail d := h.Detail
err := d.BuildDetailData()
if err != nil {
d.SetErr(err)
}
if d.Post.Thumbnail.Path != "" { if d.Post.Thumbnail.Path != "" {
img := wpconfig.Thumbnail(d.Post.Thumbnail.OriginAttachmentData, "full", "", "thumbnail", "post-thumbnail") img := wpconfig.Thumbnail(d.Post.Thumbnail.OriginAttachmentData, "full", "", "thumbnail", "post-thumbnail")
img.Sizes = "100vw" img.Sizes = "100vw"

View File

@ -109,8 +109,12 @@ func DetailRender(h *Handle) {
d.ginH["post"] = d.Post d.ginH["post"] = d.Post
} }
func Details(h *Handle) { func Detail(h *Handle) {
_ = h.Detail.BuildDetailData() err := h.Detail.BuildDetailData()
if err != nil {
h.Detail.SetErr(err)
}
h.SetData("scene", h.Scene())
} }
func ReplyCommentJs(h *Handle) { func ReplyCommentJs(h *Handle) {

View File

@ -43,6 +43,13 @@ func NewIndexHandle(handle *Handle) *IndexHandle {
return &IndexHandle{Handle: handle} return &IndexHandle{Handle: handle}
} }
func PushIndexHandler(pipeScene string, h *Handle, call HandleCall) {
h.PushHandlers(pipeScene, call, constraints.Home,
constraints.Category, constraints.Search, constraints.Tag,
constraints.Archive, constraints.Author,
)
}
func (i *IndexHandle) ParseIndex(parm *IndexParams) (err error) { func (i *IndexHandle) ParseIndex(parm *IndexParams) (err error) {
i.Param = parm i.Param = parm
switch i.scene { switch i.scene {
@ -138,21 +145,19 @@ func (i *IndexHandle) ExecPostsPlugin() {
} }
func IndexRender(h *Handle) { func IndexRender(h *Handle) {
if h.scene == constraints.Detail || h.Stats != constraints.Ok {
return
}
i := h.Index i := h.Index
i.ExecPostsPlugin() i.ExecPostsPlugin()
i.Pagination() i.Pagination()
i.ginH["posts"] = i.Posts i.ginH["posts"] = i.Posts
} }
func Indexs(h *Handle) { func Index(h *Handle) {
if h.Scene() == constraints.Detail {
return
}
i := h.Index i := h.Index
_ = i.BuildIndexData(NewIndexParams(i.C)) err := i.BuildIndexData(NewIndexParams(i.C))
if err != nil {
i.SetErr(err)
}
h.SetData("scene", h.Scene())
} }
func (i *IndexHandle) MarkSticky(posts *[]models.Posts) { func (i *IndexHandle) MarkSticky(posts *[]models.Posts) {

View File

@ -1,15 +1,12 @@
package wp package wp
import ( import (
"errors"
"github.com/fthvgb1/wp-go/app/pkg/config" "github.com/fthvgb1/wp-go/app/pkg/config"
"github.com/fthvgb1/wp-go/app/pkg/logs"
"github.com/fthvgb1/wp-go/app/pkg/models" "github.com/fthvgb1/wp-go/app/pkg/models"
"github.com/fthvgb1/wp-go/app/plugins" "github.com/fthvgb1/wp-go/app/plugins"
"github.com/fthvgb1/wp-go/app/plugins/wpposts" "github.com/fthvgb1/wp-go/app/plugins/wpposts"
"github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/maps"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings"
) )
type PostsPlugin func(*Handle, *models.Posts) type PostsPlugin func(*Handle, *models.Posts)
@ -75,7 +72,6 @@ func GetListPostPlugins(name []string, m map[string]func(PostsPlugin, *Handle, *
if ok { if ok {
return v, true return v, true
} }
logs.IfError(errors.New(str.Join("插件", t, "不存在")), "")
return nil, false return nil, false
}) })
} }

View File

@ -3,6 +3,7 @@ package wp
import ( import (
"github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/app/pkg/constraints" "github.com/fthvgb1/wp-go/app/pkg/constraints"
"github.com/fthvgb1/wp-go/helper"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings" str "github.com/fthvgb1/wp-go/helper/strings"
) )
@ -102,7 +103,9 @@ func PipeKey(h *Handle, pipScene string) string {
} }
func Run(h *Handle, conf func(*Handle)) { func Run(h *Handle, conf func(*Handle)) {
if !helper.GetContextVal(h.C, "inited", false) {
InitHandle(conf, h) InitHandle(conf, h)
}
reload.GetAnyValBys(str.Join("pipeInit-", h.scene), h, func(h *Handle) func(*Handle) { reload.GetAnyValBys(str.Join("pipeInit-", h.scene), h, func(h *Handle) func(*Handle) {
p := GetFn[Pipe]("pipe", constraints.AllScene) p := GetFn[Pipe]("pipe", constraints.AllScene)
p = append(p, GetFn[Pipe]("pipe", h.scene)...) p = append(p, GetFn[Pipe]("pipe", h.scene)...)

View File

@ -1,7 +1,6 @@
package wp package wp
import ( import (
"fmt"
"github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/app/pkg/constraints" "github.com/fthvgb1/wp-go/app/pkg/constraints"
"github.com/fthvgb1/wp-go/app/pkg/logs" "github.com/fthvgb1/wp-go/app/pkg/logs"
@ -39,6 +38,14 @@ type Handle struct {
template *template.Template template *template.Template
} }
func (h *Handle) GinH() gin.H {
return h.ginH
}
func (h *Handle) SetScene(scene string) {
h.scene = scene
}
func (h *Handle) Components() map[string]map[string][]Components[string] { func (h *Handle) Components() map[string]map[string][]Components[string] {
return h.components return h.components
} }
@ -94,6 +101,7 @@ func InitHandle(fn func(*Handle), h *Handle) {
fnMap = map[string]map[string]any{} fnMap = map[string]map[string]any{}
fnHook = map[string]map[string]any{} fnHook = map[string]map[string]any{}
fn(h) fn(h)
h.C.Set("inited", true)
inited = true inited = true
return *h return *h
}) })
@ -113,6 +121,7 @@ func InitHandle(fn func(*Handle), h *Handle) {
h.componentHook = hh.componentHook h.componentHook = hh.componentHook
h.componentsArgs = hh.componentsArgs h.componentsArgs = hh.componentsArgs
h.componentFilterFn = hh.componentFilterFn h.componentFilterFn = hh.componentFilterFn
h.C.Set("inited", true)
} }
func (h *Handle) Abort() { func (h *Handle) Abort() {
@ -195,13 +204,35 @@ func PreCodeAndStats(h *Handle) {
} }
} }
var htmlContentType = []string{"text/html; charset=utf-8"}
func (h *Handle) RenderHtml(t *template.Template, statsCode int, name string) {
header := h.C.Writer.Header()
if val := header["Content-Type"]; len(val) == 0 {
header["Content-Type"] = htmlContentType
}
h.C.Status(statsCode)
err := t.ExecuteTemplate(h.C.Writer, name, h.ginH)
h.Abort()
h.StopPipe()
if err != nil {
panic(err)
}
}
func (h *Handle) PushHandlers(pipeScene string, call HandleCall, statsOrScene ...string) {
for _, s := range statsOrScene {
h.PushHandler(pipeScene, s, call)
}
}
func (h *Handle) CommonComponents() { func (h *Handle) CommonComponents() {
h.PushCacheGroupHeadScript(constraints.AllScene, "siteIconAndCustomCss", 0, CalSiteIcon, CalCustomCss) h.PushCacheGroupHeadScript(constraints.AllScene, "siteIconAndCustomCss", 0, CalSiteIcon, CalCustomCss)
h.PushRender(constraints.AllStats, NewHandleFn(CalComponents, 10, "wp.CalComponents")) h.PushRender(constraints.AllStats, NewHandleFn(CalComponents, 10, "wp.CalComponents"))
h.PushRender(constraints.AllStats, NewHandleFn(RenderTemplate, 0, "wp.RenderTemplate")) h.PushRender(constraints.AllStats, NewHandleFn(PreRenderTemplate, 0, "wp.PreRenderTemplate"))
} }
func RenderTemplate(h *Handle) { func PreRenderTemplate(h *Handle) {
h.C.HTML(h.Code, h.templ, h.ginH) h.C.HTML(h.Code, h.templ, h.ginH)
h.StopPipe() h.StopPipe()
} }
@ -211,5 +242,4 @@ func NewHandleFn(fn HandleFn[*Handle], order int, name string) HandleCall {
} }
func NothingToDo(*Handle) { func NothingToDo(*Handle) {
fmt.Println("hi guys,how did you came to here? Is something wrong happened ?")
} }