Compare commits

...

4 Commits

Author SHA1 Message Date
7c571654a6 完善 2023-05-04 21:44:49 +08:00
90d2432575 完善 2023-05-04 21:44:17 +08:00
972ff7d815 添加示例 2023-05-04 21:39:29 +08:00
2d2c6445e8 完善 2023-05-04 21:06:27 +08:00
11 changed files with 102 additions and 18 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
wp-go.iml
config.yaml
err.log
plugins

View File

@ -77,6 +77,7 @@ func SetupRouter() *gin.Engine {
r.GET("/p/:id/feed", actions.PostFeed)
r.GET("/feed", actions.Feed)
r.GET("/comments/feed", actions.CommentsFeed)
//r.NoRoute(actions.ThemeHook(constraints.NoRoute))
commentMiddleWare, _ := middleware.FlowLimit(c.MaxRequestSleepNum, 5, c.CacheTime.SleepTime)
r.POST("/comment", commentMiddleWare, actions.PostComment)
if c.Pprof != "" {

View File

@ -9,6 +9,8 @@ const (
Author = "Author"
Detail = "Detail"
NoRoute = "NoRoute"
Ok = "Ok"
Error404 = "Error404"
ParamError = "ParamError"

View File

@ -0,0 +1,36 @@
module plugintt
go 1.20
require github.com/fthvgb1/wp-go latest
require (
github.com/dlclark/regexp2 v1.7.0 // indirect
github.com/elliotchance/phpserialize v1.3.3 // indirect
github.com/gin-contrib/sessions v0.0.5 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.8.1 // 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
github.com/jmoiron/sqlx v1.3.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
golang.org/x/crypto v0.0.0-20220924013350-4ba4fb4dd9e7 // indirect
golang.org/x/exp v0.0.0-20230203172020-98cc5a0785f9 // indirect
golang.org/x/net v0.5.0 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/text v0.6.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

View File

@ -0,0 +1,24 @@
package main
import (
"github.com/fthvgb1/wp-go/app/pkg/constraints"
"github.com/fthvgb1/wp-go/app/theme"
"github.com/fthvgb1/wp-go/app/theme/wp"
"plugintt/xx"
)
func init() {
// here can register theme
theme.AddThemeHookFunc("xxx", Xo)
}
func Xo(h *wp.Handle) {
// action or hook or config theme
h.PushHandler(constraints.PipeRender, constraints.Home, wp.HandleCall{
Fn: func(handle *wp.Handle) {
xx.Xo()
},
Order: 100,
Name: "xxxx",
})
}

View File

@ -0,0 +1,6 @@
#/bin/bash
# copy plugintt to other dir and remove .dev suffix
# note the go version and build tool flag must same to server build
# eg: -gcflags all="-N -l" may used in ide debug
go build -buildmode=plugin -o xx.so main.go

View File

@ -0,0 +1,11 @@
package xx
import (
"fmt"
"github.com/shopspring/decimal"
)
func Xo() {
fmt.Println("xxoo")
fmt.Println(decimal.Max(decimal.NewFromFloat(32.3333333333333), decimal.NewFromFloat(32.33333333333331)).String())
}

View File

@ -45,22 +45,22 @@ func LoadPlugins() {
return
}
for _, entry := range glob {
f := filepath.Join(dirPath, entry)
p, err := plugin.Open(f)
p, err := plugin.Open(entry)
if err != nil {
logs.Error(err, "读取插件错误", f)
logs.Error(err, "读取插件错误", entry)
continue
}
name := filepath.Ext(entry)
name = str.FirstUpper(entry[0 : len(entry)-len(name)])
name = filepath.Base(entry[0 : len(entry)-len(name)])
name = str.FirstUpper(name)
pl, err := p.Lookup(name)
if err != nil {
logs.Error(err, "插件lookup错误", f)
logs.Error(err, "插件lookup错误", entry)
continue
}
plu, ok := pl.(func(*wp.Handle))
if !ok {
logs.Error(errors.New("switch func(*wp.Handle) fail"), "插件转换错误", f)
logs.Error(errors.New("switch func(*wp.Handle) fail"), "插件转换错误", entry)
continue
}
RegisterPlugin(name, plu)

View File

@ -1,24 +1,25 @@
package theme
import (
"github.com/fthvgb1/wp-go/app/theme/twentyfifteen"
"github.com/fthvgb1/wp-go/app/theme/wp"
"github.com/fthvgb1/wp-go/safety"
"net/http"
)
var themeMap = map[string]func(*wp.Handle){}
var themeMap = safety.NewMap[string, func(*wp.Handle)]()
func addThemeHookFunc(name string, fn func(handle *wp.Handle)) {
if _, ok := themeMap[name]; ok {
func AddThemeHookFunc(name string, fn func(handle *wp.Handle)) {
if _, ok := themeMap.Load(name); ok {
panic("exists same name theme")
}
themeMap[name] = fn
themeMap.Store(name, fn)
}
func Hook(themeName string, handle *wp.Handle) {
fn, ok := themeMap[themeName]
func Hook(themeName string, h *wp.Handle) {
fn, ok := themeMap.Load(themeName)
if ok && fn != nil {
fn(handle)
fn(h)
return
}
themeMap[twentyfifteen.ThemeName](handle)
h.C.String(http.StatusNotFound, "404")
}

View File

@ -8,9 +8,9 @@ import (
)
func InitTheme() {
addThemeHookFunc(twentyfifteen.ThemeName, twentyfifteen.Hook)
AddThemeHookFunc(twentyfifteen.ThemeName, twentyfifteen.Hook)
twentyfifteen.Init(TemplateFs)
addThemeHookFunc(twentyseventeen.ThemeName, twentyseventeen.Hook)
AddThemeHookFunc(twentyseventeen.ThemeName, twentyseventeen.Hook)
twentyseventeen.Init(TemplateFs)
}

View File

@ -92,7 +92,9 @@ postOrder: "desc"
uploadDir: ""
# pprof route path 为空表示不开启pprof,否则为pprof的路由
pprof: "/debug/pprof"
# 程序插件
# 要使用的程序插件
plugins: ["enlightjs"]
# 插件存放路径
pluginPath: "./plugins"
# 列表页面post使用的插件
listPagePlugins: ["passwordProject","digest","twentyseventeen_postThumbnail"]