diff --git a/go.mod b/go.mod index cce3a1e..9995dd6 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,11 @@ require ( github.com/gin-contrib/pprof v1.4.0 github.com/gin-contrib/sessions v0.0.5 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/goccy/go-json v0.9.11 github.com/jmoiron/sqlx v1.3.5 github.com/soxfmr/gomail v0.0.0-20200806033254-80bf84e583f0 golang.org/x/crypto v0.0.0-20220924013350-4ba4fb4dd9e7 @@ -19,10 +23,6 @@ require ( require ( 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/securecookie v1.1.1 // indirect github.com/gorilla/sessions v1.2.1 // indirect diff --git a/helper/strings/strings.go b/helper/strings/strings.go index 568cfa9..203fa01 100644 --- a/helper/strings/strings.go +++ b/helper/strings/strings.go @@ -23,6 +23,13 @@ func Join(s ...string) (str string) { 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 { if s == "" { return defaults diff --git a/internal/cmd/main.go b/internal/cmd/main.go index d69fc33..57d0c67 100644 --- a/internal/cmd/main.go +++ b/internal/cmd/main.go @@ -12,6 +12,7 @@ import ( "github.com/fthvgb1/wp-go/internal/pkg/db" "github.com/fthvgb1/wp-go/internal/pkg/logs" "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/wpconfig" "github.com/fthvgb1/wp-go/model" @@ -74,6 +75,7 @@ func initConf(c string) (err error) { if err != nil { return } + wphandle.LoadPlugins() return } @@ -114,6 +116,7 @@ func reloads() { logs.IfError(err, "获取网站设置WpOption失败") err = wpconfig.InitTerms() logs.IfError(err, "获取WpTerms表失败") + wphandle.LoadPlugins() reload.Reload() flushCache() log.Println("reload complete") diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index 20ad2c1..aec012c 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -19,6 +19,8 @@ type Config struct { Mysql Mysql `yaml:"mysql" json:"mysql"` Mail Mail `yaml:"mail" json:"mail"` 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"` DigestAllowTag string `yaml:"digestAllowTag" json:"digestAllowTag"` MaxRequestSleepNum int64 `yaml:"maxRequestSleepNum" json:"maxRequestSleepNum,omitempty"` diff --git a/internal/plugins/wphandle/handle.go b/internal/plugins/wphandle/handle.go index 89249d5..f81523d 100644 --- a/internal/plugins/wphandle/handle.go +++ b/internal/plugins/wphandle/handle.go @@ -1,32 +1,68 @@ package wphandle 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/logs" "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/tests" "github.com/fthvgb1/wp-go/internal/theme/wp" + "github.com/fthvgb1/wp-go/safety" + "path/filepath" + "plugin" ) -var plugins = wp.HandlePlugins{ - "enlightjs": enlightjs.EnlighterJS, - "hiddenLogin": hiddenlogin.HiddenLogin, - "test": tests.Tt, -} +var plugins = func() *safety.Map[string, func(*wp.Handle)] { + m := safety.NewMap[string, func(*wp.Handle)]() + m.Store("enlightjs", enlightjs.EnlighterJS) + m.Store("hiddenLogin", hiddenlogin.HiddenLogin) + return m +}() -func RegisterPlugins(m wp.HandlePlugins) { - for k, v := range m { - if _, ok := plugins[k]; !ok { - plugins[k] = v - } - } +func RegisterPlugin(name string, fn func(*wp.Handle)) { + plugins.Store(name, fn) } func UsePlugins(h *wp.Handle, calls ...string) { calls = append(calls, config.GetConfig().Plugins...) for _, call := range calls { - if fn, ok := plugins[call]; ok { + if fn, ok := plugins.Load(call); ok { 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) + } + +}