插件支持配置、热加载
This commit is contained in:
parent
3069bc3892
commit
daa9a26e00
|
@ -88,4 +88,6 @@ postOrder: "desc"
|
|||
# 上传的目录
|
||||
uploadDir: ""
|
||||
# pprof route path 为空表示不开启pprof,否则为pprof的路由
|
||||
pprof: "/debug/pprof"
|
||||
pprof: "/debug/pprof"
|
||||
# 列表页面post使用的插件
|
||||
listPagePlugins: ["passwordProject","digest","twentyseventeen_postThumbnail"]
|
|
@ -85,3 +85,26 @@ func Replace[K comparable, V any](m map[K]V, mm ...map[K]V) map[K]V {
|
|||
}
|
||||
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
|
||||
}
|
||||
|
|
|
@ -31,6 +31,7 @@ type Config struct {
|
|||
PostOrder string `yaml:"postOrder"`
|
||||
UploadDir string `yaml:"uploadDir"`
|
||||
Pprof string `yaml:"pprof"`
|
||||
ListPagePlugins []string `yaml:"listPagePlugins"`
|
||||
}
|
||||
|
||||
type CacheTime struct {
|
||||
|
|
|
@ -2,8 +2,12 @@ package common
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/fthvgb1/wp-go/helper/maps"
|
||||
"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/logs"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||
"github.com/fthvgb1/wp-go/internal/plugins"
|
||||
"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 Plugin[T any] func(next Fn[T], h Handle, t T) T
|
||||
|
||||
|
@ -41,14 +53,40 @@ var plugin = []Plugin[models.Posts]{
|
|||
PasswordProject,
|
||||
}
|
||||
|
||||
func Plugins() []Plugin[models.Posts] {
|
||||
return slice.Copy(plugin)
|
||||
var pluginFns = map[string]Plugin[models.Posts]{
|
||||
"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 {
|
||||
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) {
|
||||
r = post
|
||||
if post.PostPassword != "" {
|
||||
|
@ -69,6 +107,7 @@ func ProjectTitle(t models.Posts) models.Posts {
|
|||
return t
|
||||
}
|
||||
|
||||
// Digest 生成摘要
|
||||
func Digest(next Fn[models.Posts], h Handle, post models.Posts) models.Posts {
|
||||
if post.PostExcerpt != "" {
|
||||
plugins.PostExcerpt(&post)
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package twentyfifteen
|
||||
|
||||
import (
|
||||
"github.com/fthvgb1/wp-go/helper/slice"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||
"github.com/fthvgb1/wp-go/internal/plugins"
|
||||
"github.com/fthvgb1/wp-go/internal/theme/common"
|
||||
|
@ -37,9 +36,7 @@ var plugin = common.Plugins()
|
|||
func (h handle) Index() {
|
||||
if h.Stats != plugins.Empty404 {
|
||||
|
||||
h.GinH["posts"] = slice.Map(
|
||||
h.GinH["posts"].([]models.Posts),
|
||||
common.PluginFn[models.Posts](plugin, h.Handle, common.DigestsAndOthers(h.C)))
|
||||
h.ExecListPagePlugin(plugin)
|
||||
|
||||
p, ok := h.GinH["pagination"]
|
||||
if ok {
|
||||
|
|
|
@ -2,6 +2,7 @@ package twentyseventeen
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/fthvgb1/wp-go/helper/maps"
|
||||
"github.com/fthvgb1/wp-go/helper/slice"
|
||||
str "github.com/fthvgb1/wp-go/helper/strings"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/cache"
|
||||
|
@ -44,17 +45,15 @@ func Hook(cHandle common.Handle) {
|
|||
h.Index()
|
||||
}
|
||||
|
||||
var plugin = func() []common.Plugin[models.Posts] {
|
||||
return append(common.Plugins(), postThumbnail)
|
||||
var pluginFns = func() map[string]common.Plugin[models.Posts] {
|
||||
return maps.Merge(common.Plugins(), map[string]common.Plugin[models.Posts]{
|
||||
"twentyseventeen_postThumbnail": postThumbnail,
|
||||
})
|
||||
}()
|
||||
|
||||
func (h handle) Index() {
|
||||
if h.Stats != plugins.Empty404 {
|
||||
|
||||
h.GinH["posts"] = slice.Map(
|
||||
h.GinH["posts"].([]models.Posts),
|
||||
common.PluginFn[models.Posts](plugin, h.Handle, common.DigestsAndOthers(h.C)))
|
||||
|
||||
h.ExecListPagePlugin(pluginFns)
|
||||
p, ok := h.GinH["pagination"]
|
||||
if ok {
|
||||
pp, ok := p.(pagination.ParsePagination)
|
||||
|
|
Loading…
Reference in New Issue
Block a user