ag/templates/_plugin.go

48 lines
1.3 KiB
Go
Raw Permalink Normal View History

2024-03-15 22:32:19 +08:00
package {{.name}}
import (
2024-06-26 12:16:21 +08:00
"apigo.cc/apigo/plugin"
2024-03-15 22:32:19 +08:00
"errors"
"github.com/ssgo/log"
"github.com/ssgo/u"
)
var prefix = ""
func init() {
plugin.Register(plugin.Plugin{
Id: "{{.name}}",
Name: "",
// set plugin contents
Objects: map[string]interface{}{
// *plugin.Context is optional if need to use, ignore it when invoke
"set": func(key string, value interface{}, ctx *plugin.Context) {
ctx.SetData(prefix+key, value)
},
// return error is optional if need to throw exception, ignore it when invoke
"get": func(key string, ctx *plugin.Context) (interface{}, error) {
value := ctx.GetData(prefix + key)
if value == nil {
return nil, errors.New("data " + key + " is null")
}
return value, nil
},
// you can get inject object from *plugin.Context, *log.Logger is default logger with a unique trance id
"remove": func(key string, ctx *plugin.Context) {
if logger, ok := ctx.GetInject("*log.Logger").(*log.Logger); ok {
logger.Warning("context data cannot be remove, will set to null")
}
ctx.SetData(prefix+key, nil)
},
},
// set config sample (recommend to use YAML format)
ConfigSample: `prefix: _`,
// init plugin config
Init: func(config map[string]interface{}) {
if config[prefix] != nil {
prefix = u.String(config[prefix])
}
},
})
}