This commit is contained in:
xing 2023-05-04 19:46:06 +08:00
parent 4323c508ba
commit 0d3481f722
5 changed files with 65 additions and 17 deletions

8
go.mod
View File

@ -9,7 +9,11 @@ require (
github.com/gin-contrib/pprof v1.4.0 github.com/gin-contrib/pprof v1.4.0
github.com/gin-contrib/sessions v0.0.5 github.com/gin-contrib/sessions v0.0.5
github.com/gin-gonic/gin v1.8.1 github.com/gin-gonic/gin v1.8.1
github.com/go-playground/locales v0.14.0
github.com/go-playground/universal-translator v0.18.0
github.com/go-playground/validator/v10 v10.11.1
github.com/go-sql-driver/mysql v1.6.0 github.com/go-sql-driver/mysql v1.6.0
github.com/goccy/go-json v0.9.11
github.com/jmoiron/sqlx v1.3.5 github.com/jmoiron/sqlx v1.3.5
github.com/soxfmr/gomail v0.0.0-20200806033254-80bf84e583f0 github.com/soxfmr/gomail v0.0.0-20200806033254-80bf84e583f0
golang.org/x/crypto v0.0.0-20220924013350-4ba4fb4dd9e7 golang.org/x/crypto v0.0.0-20220924013350-4ba4fb4dd9e7
@ -19,10 +23,6 @@ require (
require ( require (
github.com/gin-contrib/sse v0.1.0 // indirect github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.11.1 // indirect
github.com/goccy/go-json v0.9.11 // indirect
github.com/gorilla/context v1.1.1 // indirect github.com/gorilla/context v1.1.1 // indirect
github.com/gorilla/securecookie v1.1.1 // indirect github.com/gorilla/securecookie v1.1.1 // indirect
github.com/gorilla/sessions v1.2.1 // indirect github.com/gorilla/sessions v1.2.1 // indirect

View File

@ -23,6 +23,13 @@ func Join(s ...string) (str string) {
return return
} }
func FirstUpper(s string) string {
if len(s) < 1 {
return s
}
return Join(strings.ToUpper(s[:1]), s[1:])
}
func ToInteger[T constraints.Integer](s string, defaults T) T { func ToInteger[T constraints.Integer](s string, defaults T) T {
if s == "" { if s == "" {
return defaults return defaults

View File

@ -12,6 +12,7 @@ import (
"github.com/fthvgb1/wp-go/internal/pkg/db" "github.com/fthvgb1/wp-go/internal/pkg/db"
"github.com/fthvgb1/wp-go/internal/pkg/logs" "github.com/fthvgb1/wp-go/internal/pkg/logs"
"github.com/fthvgb1/wp-go/internal/plugins" "github.com/fthvgb1/wp-go/internal/plugins"
"github.com/fthvgb1/wp-go/internal/plugins/wphandle"
"github.com/fthvgb1/wp-go/internal/theme" "github.com/fthvgb1/wp-go/internal/theme"
"github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/model" "github.com/fthvgb1/wp-go/model"
@ -74,6 +75,7 @@ func initConf(c string) (err error) {
if err != nil { if err != nil {
return return
} }
wphandle.LoadPlugins()
return return
} }
@ -114,6 +116,7 @@ func reloads() {
logs.IfError(err, "获取网站设置WpOption失败") logs.IfError(err, "获取网站设置WpOption失败")
err = wpconfig.InitTerms() err = wpconfig.InitTerms()
logs.IfError(err, "获取WpTerms表失败") logs.IfError(err, "获取WpTerms表失败")
wphandle.LoadPlugins()
reload.Reload() reload.Reload()
flushCache() flushCache()
log.Println("reload complete") log.Println("reload complete")

View File

@ -19,6 +19,8 @@ type Config struct {
Mysql Mysql `yaml:"mysql" json:"mysql"` Mysql Mysql `yaml:"mysql" json:"mysql"`
Mail Mail `yaml:"mail" json:"mail"` Mail Mail `yaml:"mail" json:"mail"`
CacheTime CacheTime `yaml:"cacheTime" json:"cacheTime"` CacheTime CacheTime `yaml:"cacheTime" json:"cacheTime"`
PluginPath string `yaml:"pluginPath" json:"pluginPath"`
ExternScript []string `json:"externScript" yaml:"externScript"`
DigestWordCount int `yaml:"digestWordCount" json:"digestWordCount,omitempty"` DigestWordCount int `yaml:"digestWordCount" json:"digestWordCount,omitempty"`
DigestAllowTag string `yaml:"digestAllowTag" json:"digestAllowTag"` DigestAllowTag string `yaml:"digestAllowTag" json:"digestAllowTag"`
MaxRequestSleepNum int64 `yaml:"maxRequestSleepNum" json:"maxRequestSleepNum,omitempty"` MaxRequestSleepNum int64 `yaml:"maxRequestSleepNum" json:"maxRequestSleepNum,omitempty"`

View File

@ -1,32 +1,68 @@
package wphandle package wphandle
import ( import (
"errors"
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/plugins/wphandle/enlightjs" "github.com/fthvgb1/wp-go/internal/plugins/wphandle/enlightjs"
"github.com/fthvgb1/wp-go/internal/plugins/wphandle/hiddenlogin" "github.com/fthvgb1/wp-go/internal/plugins/wphandle/hiddenlogin"
"github.com/fthvgb1/wp-go/internal/plugins/wphandle/tests"
"github.com/fthvgb1/wp-go/internal/theme/wp" "github.com/fthvgb1/wp-go/internal/theme/wp"
"github.com/fthvgb1/wp-go/safety"
"path/filepath"
"plugin"
) )
var plugins = wp.HandlePlugins{ var plugins = func() *safety.Map[string, func(*wp.Handle)] {
"enlightjs": enlightjs.EnlighterJS, m := safety.NewMap[string, func(*wp.Handle)]()
"hiddenLogin": hiddenlogin.HiddenLogin, m.Store("enlightjs", enlightjs.EnlighterJS)
"test": tests.Tt, m.Store("hiddenLogin", hiddenlogin.HiddenLogin)
} return m
}()
func RegisterPlugins(m wp.HandlePlugins) { func RegisterPlugin(name string, fn func(*wp.Handle)) {
for k, v := range m { plugins.Store(name, fn)
if _, ok := plugins[k]; !ok {
plugins[k] = v
}
}
} }
func UsePlugins(h *wp.Handle, calls ...string) { func UsePlugins(h *wp.Handle, calls ...string) {
calls = append(calls, config.GetConfig().Plugins...) calls = append(calls, config.GetConfig().Plugins...)
for _, call := range calls { for _, call := range calls {
if fn, ok := plugins[call]; ok { if fn, ok := plugins.Load(call); ok {
fn(h) fn(h)
} }
} }
} }
func LoadPlugins() {
dirPath := config.GetConfig().PluginPath
if dirPath == "" {
return
}
glob, err := filepath.Glob(filepath.Join(dirPath, "*.so"))
if err != nil {
logs.Error(err, "读取插件目录错误", dirPath)
return
}
for _, entry := range glob {
f := filepath.Join(dirPath, entry)
p, err := plugin.Open(f)
if err != nil {
logs.Error(err, "读取插件错误", f)
continue
}
name := filepath.Ext(entry)
name = str.FirstUpper(entry[0 : len(entry)-len(name)])
pl, err := p.Lookup(name)
if err != nil {
logs.Error(err, "插件lookup错误", f)
continue
}
plu, ok := pl.(func(*wp.Handle))
if !ok {
logs.Error(errors.New("switch func(*wp.Handle) fail"), "插件转换错误", f)
continue
}
RegisterPlugin(name, plu)
}
}