插件支持配置、热加载

This commit is contained in:
xing 2023-02-09 23:15:10 +08:00
parent 3069bc3892
commit daa9a26e00
6 changed files with 75 additions and 14 deletions

View File

@ -89,3 +89,5 @@ postOrder: "desc"
uploadDir: "" uploadDir: ""
# pprof route path 为空表示不开启pprof,否则为pprof的路由 # pprof route path 为空表示不开启pprof,否则为pprof的路由
pprof: "/debug/pprof" pprof: "/debug/pprof"
# 列表页面post使用的插件
listPagePlugins: ["passwordProject","digest","twentyseventeen_postThumbnail"]

View File

@ -85,3 +85,26 @@ func Replace[K comparable, V any](m map[K]V, mm ...map[K]V) map[K]V {
} }
return m return m
} }
func Copy[K comparable, V any](m map[K]V) map[K]V {
r := make(map[K]V)
for k, v := range m {
r[k] = v
}
return r
}
func Merge[K comparable, V any](m ...map[K]V) map[K]V {
if len(m) < 1 {
panic("no map")
} else if len(m) < 2 {
return m[0]
}
mm := m[0]
for _, m2 := range m[1:] {
for k, v := range m2 {
mm[k] = v
}
}
return mm
}

View File

@ -31,6 +31,7 @@ type Config struct {
PostOrder string `yaml:"postOrder"` PostOrder string `yaml:"postOrder"`
UploadDir string `yaml:"uploadDir"` UploadDir string `yaml:"uploadDir"`
Pprof string `yaml:"pprof"` Pprof string `yaml:"pprof"`
ListPagePlugins []string `yaml:"listPagePlugins"`
} }
type CacheTime struct { type CacheTime struct {

View File

@ -2,8 +2,12 @@ package common
import ( import (
"context" "context"
"errors"
"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"
"github.com/fthvgb1/wp-go/internal/pkg/config" "github.com/fthvgb1/wp-go/internal/pkg/config"
"github.com/fthvgb1/wp-go/internal/pkg/logs"
"github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/plugins" "github.com/fthvgb1/wp-go/internal/plugins"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -26,6 +30,14 @@ func (h Handle) Index() {
} }
func (h Handle) ExecListPagePlugin(m map[string]Plugin[models.Posts], calls ...func(*models.Posts)) {
pluginConf := config.GetConfig().ListPagePlugins
plugin := GetPlugins(pluginConf, m)
h.GinH["posts"] = slice.Map(
h.GinH["posts"].([]models.Posts),
PluginFn[models.Posts](plugin, h, Defaults(calls...)))
}
type Fn[T any] func(T) T type Fn[T any] func(T) T
type Plugin[T any] func(next Fn[T], h Handle, t T) T type Plugin[T any] func(next Fn[T], h Handle, t T) T
@ -41,14 +53,40 @@ var plugin = []Plugin[models.Posts]{
PasswordProject, PasswordProject,
} }
func Plugins() []Plugin[models.Posts] { var pluginFns = map[string]Plugin[models.Posts]{
return slice.Copy(plugin) "passwordProject": PasswordProject,
"digest": Digest,
}
func Plugins() map[string]Plugin[models.Posts] {
return maps.Copy(pluginFns)
}
func Defaults(call ...func(*models.Posts)) Fn[models.Posts] {
return func(posts models.Posts) models.Posts {
for _, fn := range call {
fn(&posts)
}
return posts
}
} }
func Default[T any](t T) T { func Default[T any](t T) T {
return t return t
} }
func GetPlugins(name []string, m map[string]Plugin[models.Posts]) []Plugin[models.Posts] {
return slice.FilterAndMap(name, func(t string) (Plugin[models.Posts], bool) {
v, ok := m[t]
if ok {
return v, true
}
logs.ErrPrintln(errors.New(str.Join("插件", t, "不存在")), "")
return nil, false
})
}
// PasswordProject 标题和内容密码保护
func PasswordProject(next Fn[models.Posts], h Handle, post models.Posts) (r models.Posts) { func PasswordProject(next Fn[models.Posts], h Handle, post models.Posts) (r models.Posts) {
r = post r = post
if post.PostPassword != "" { if post.PostPassword != "" {
@ -69,6 +107,7 @@ func ProjectTitle(t models.Posts) models.Posts {
return t return t
} }
// Digest 生成摘要
func Digest(next Fn[models.Posts], h Handle, post models.Posts) models.Posts { func Digest(next Fn[models.Posts], h Handle, post models.Posts) models.Posts {
if post.PostExcerpt != "" { if post.PostExcerpt != "" {
plugins.PostExcerpt(&post) plugins.PostExcerpt(&post)

View File

@ -1,7 +1,6 @@
package twentyfifteen package twentyfifteen
import ( import (
"github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/plugins" "github.com/fthvgb1/wp-go/internal/plugins"
"github.com/fthvgb1/wp-go/internal/theme/common" "github.com/fthvgb1/wp-go/internal/theme/common"
@ -37,9 +36,7 @@ var plugin = common.Plugins()
func (h handle) Index() { func (h handle) Index() {
if h.Stats != plugins.Empty404 { if h.Stats != plugins.Empty404 {
h.GinH["posts"] = slice.Map( h.ExecListPagePlugin(plugin)
h.GinH["posts"].([]models.Posts),
common.PluginFn[models.Posts](plugin, h.Handle, common.DigestsAndOthers(h.C)))
p, ok := h.GinH["pagination"] p, ok := h.GinH["pagination"]
if ok { if ok {

View File

@ -2,6 +2,7 @@ package twentyseventeen
import ( import (
"fmt" "fmt"
"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" str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/pkg/cache" "github.com/fthvgb1/wp-go/internal/pkg/cache"
@ -44,17 +45,15 @@ func Hook(cHandle common.Handle) {
h.Index() h.Index()
} }
var plugin = func() []common.Plugin[models.Posts] { var pluginFns = func() map[string]common.Plugin[models.Posts] {
return append(common.Plugins(), postThumbnail) return maps.Merge(common.Plugins(), map[string]common.Plugin[models.Posts]{
"twentyseventeen_postThumbnail": postThumbnail,
})
}() }()
func (h handle) Index() { func (h handle) Index() {
if h.Stats != plugins.Empty404 { if h.Stats != plugins.Empty404 {
h.ExecListPagePlugin(pluginFns)
h.GinH["posts"] = slice.Map(
h.GinH["posts"].([]models.Posts),
common.PluginFn[models.Posts](plugin, h.Handle, common.DigestsAndOthers(h.C)))
p, ok := h.GinH["pagination"] p, ok := h.GinH["pagination"]
if ok { if ok {
pp, ok := p.(pagination.ParsePagination) pp, ok := p.(pagination.ParsePagination)