diff --git a/app/actions/themehook.go b/app/actions/themehook.go index 083c1a5..aa7d1b4 100644 --- a/app/actions/themehook.go +++ b/app/actions/themehook.go @@ -10,8 +10,6 @@ func ThemeHook(scene string) func(*gin.Context) { return func(c *gin.Context) { t := theme.GetCurrentTemplateName() h := wp.NewHandle(c, scene, t) - h.Index = wp.NewIndexHandle(h) - h.Detail = wp.NewDetailHandle(h) templ, _ := theme.GetTemplate(t) h.SetTemplate(templ) theme.Hook(t, h) diff --git a/app/middleware/flowLimit.go b/app/middleware/flowLimit.go index ad62407..811a2ee 100644 --- a/app/middleware/flowLimit.go +++ b/app/middleware/flowLimit.go @@ -19,9 +19,9 @@ func FlowLimit(maxRequestSleepNum, maxRequestNum int64, sleepTime []time.Duratio } s := safety.Var[[]time.Duration]{} s.Store(sleepTime) - fn := func(msn, mn int64, st []time.Duration) { - atomic.StoreInt64(&maxRequestSleepNum, msn) - atomic.StoreInt64(&maxRequestNum, mn) + fn := func(sleepNum, maxNum int64, st []time.Duration) { + atomic.StoreInt64(&maxRequestSleepNum, sleepNum) + atomic.StoreInt64(&maxRequestNum, maxNum) s.Store(st) } return func(c *gin.Context) { diff --git a/app/middleware/iplimit.go b/app/middleware/iplimit.go index feb58ae..48804aa 100644 --- a/app/middleware/iplimit.go +++ b/app/middleware/iplimit.go @@ -25,14 +25,25 @@ func IpLimit(num int64) (func(ctx *gin.Context), func(int64)) { fn(num) return func(c *gin.Context) { + if atomic.LoadInt64(m.limitNum) <= 0 { + c.Next() + return + } ip := c.ClientIP() - s := false m.mux.RLock() i, ok := m.m[ip] m.mux.RUnlock() + + if !ok { + m.mux.Lock() + i = new(int64) + m.m[ip] = i + m.mux.Unlock() + } + defer func() { ii := atomic.LoadInt64(i) - if s && ii > 0 { + if ii > 0 { atomic.AddInt64(i, -1) if atomic.LoadInt64(i) == 0 { m.mux.Lock() @@ -42,20 +53,12 @@ func IpLimit(num int64) (func(ctx *gin.Context), func(int64)) { } }() - if !ok { - m.mux.Lock() - i = new(int64) - m.m[ip] = i - m.mux.Unlock() - } - - if atomic.LoadInt64(m.limitNum) > 0 && atomic.LoadInt64(i) >= atomic.LoadInt64(m.limitNum) { + if atomic.LoadInt64(i) >= atomic.LoadInt64(m.limitNum) { c.Status(http.StatusForbidden) c.Abort() return } atomic.AddInt64(i, 1) - s = true c.Next() }, fn } diff --git a/app/pkg/db/db.go b/app/pkg/db/db.go index db88b78..2f6b507 100644 --- a/app/pkg/db/db.go +++ b/app/pkg/db/db.go @@ -39,9 +39,11 @@ func InitDb() (*safety.Var[*sqlx.DB], error) { if preDb != nil { _ = preDb.Close() } - showQuerySql = reload.FnVal("showQuerySql", false, func() bool { - return config.GetConfig().ShowQuerySql - }) + if showQuerySql == nil { + showQuerySql = reload.BuildFnVal("showQuerySql", false, func() bool { + return config.GetConfig().ShowQuerySql + }) + } return safeDb, err } diff --git a/app/theme/twentyfifteen/twentyfifteen.go b/app/theme/twentyfifteen/twentyfifteen.go index 28bbb4c..8ac84c6 100644 --- a/app/theme/twentyfifteen/twentyfifteen.go +++ b/app/theme/twentyfifteen/twentyfifteen.go @@ -23,7 +23,7 @@ func configs(h *wp.Handle) { }) wp.InitPipe(h) middleware.CommonMiddleware(h) - h.Index.SetPageEle(plugins.TwentyFifteenPagination()) + setPaginationAndRender(h) h.PushCacheGroupHeadScript(constraints.AllScene, "CalCustomBackGround", 10.005, CalCustomBackGround) h.PushCacheGroupHeadScript(constraints.AllScene, "colorSchemeCss", 10.0056, colorSchemeCss) h.CommonComponents() @@ -39,8 +39,22 @@ func configs(h *wp.Handle) { h.PushRender(constraints.AllScene, wp.NewHandleFn(wp.PreTemplate, 70.005, "wp.PreTemplate")) } +func setPaginationAndRender(h *wp.Handle) { + h.PushHandler(constraints.PipeRender, constraints.Detail, wp.NewHandleFn(func(hh *wp.Handle) { + d := hh.GetDetailHandle() + d.CommentRender = plugins.CommentRender() + d.CommentPageEle = plugins.TwentyFifteenCommentPagination() + }, 150, "setPaginationAndRender")) + + wp.PushIndexHandler(constraints.PipeRender, h, wp.NewHandleFn(func(hh *wp.Handle) { + i := hh.GetIndexHandle() + i.SetPageEle(plugins.TwentyFifteenPagination()) + }, 150, "setPaginationAndRender")) +} + func postThumb(h *wp.Handle) { - if h.Detail.Post.Thumbnail.Path != "" { - h.Detail.Post.Thumbnail = wpconfig.Thumbnail(h.Detail.Post.Thumbnail.OriginAttachmentData, "post-thumbnail", "") + d := h.GetDetailHandle() + if d.Post.Thumbnail.Path != "" { + d.Post.Thumbnail = wpconfig.Thumbnail(d.Post.Thumbnail.OriginAttachmentData, "post-thumbnail", "") } } diff --git a/app/theme/twentyseventeen/twentyseventeen.go b/app/theme/twentyseventeen/twentyseventeen.go index 33d76fc..6cbf588 100644 --- a/app/theme/twentyseventeen/twentyseventeen.go +++ b/app/theme/twentyseventeen/twentyseventeen.go @@ -57,7 +57,7 @@ func configs(h *wp.Handle) { components.WidgetArea(h) pushScripts(h) h.PushRender(constraints.AllStats, wp.NewHandleFn(calCustomHeader, 10.005, "calCustomHeader")) - wp.SetComponentsArgs(h, widgets.Widget, map[string]string{ + wp.SetComponentsArgs(widgets.Widget, map[string]string{ "{$before_widget}": `
`, "{$after_widget}": `
`, }) @@ -67,13 +67,11 @@ func configs(h *wp.Handle) { ) videoHeader(h) h.SetData("colophon", colophon) - h.Detail.CommentRender = commentFormat - h.Detail.CommentPageEle = commentPageEle + setPaginationAndRender(h) h.CommonComponents() - h.Index.SetPageEle(paginate) wp.ReplyCommentJs(h) h.PushPostPlugin(postThumbnail) - wp.SetComponentsArgsForMap(h, widgets.Search, "{$form}", searchForm) + wp.SetComponentsArgsForMap(widgets.Search, "{$form}", searchForm) wp.PushIndexHandler(constraints.PipeRender, h, wp.NewHandleFn(wp.IndexRender, 10.005, "wp.IndexRender")) h.PushRender(constraints.Detail, wp.NewHandleFn(wp.DetailRender, 10.005, "wp.DetailRender")) h.PushDataHandler(constraints.Detail, wp.NewHandleFn(wp.Detail, 100.005, "wp.Detail"), wp.NewHandleFn(postThumb, 90.005, "{theme}.postThumb")) @@ -81,6 +79,18 @@ func configs(h *wp.Handle) { h.PushDataHandler(constraints.AllScene, wp.NewHandleFn(wp.PreCodeAndStats, 90.005, "wp.PreCodeAndStats")) } +func setPaginationAndRender(h *wp.Handle) { + h.PushHandler(constraints.PipeRender, constraints.Detail, wp.NewHandleFn(func(hh *wp.Handle) { + d := hh.GetDetailHandle() + d.CommentRender = commentFormat + d.CommentPageEle = commentPageEle + }, 150, "setPaginationAndRender")) + wp.PushIndexHandler(constraints.PipeRender, h, wp.NewHandleFn(func(hh *wp.Handle) { + i := hh.GetIndexHandle() + i.SetPageEle(paginate) + }, 150, "setPaginationAndRender")) +} + var searchForm = `