plugin: init the structure of the plugins
This commit adds a basic configuration for the plugin structure Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
This commit is contained in:
parent
8e9dde5159
commit
b7799b7116
|
@ -7,10 +7,11 @@ import (
|
|||
)
|
||||
|
||||
func main() {
|
||||
state := core.PluginState{}
|
||||
plugin := plugin.New(&state, true, plugin.DummyOnInit[*core.PluginState])
|
||||
plugin.RegisterOption("foo", "string", "Hello Go", "An example of option", false)
|
||||
plugin.RegisterRPCMethod("hello", "", "an example of rpc method", core.Hello)
|
||||
state := core.State{}
|
||||
plugin := plugin.New(&state, true, core.OnInit)
|
||||
plugin.RegisterNotification("shutdown", core.OnShutdown)
|
||||
|
||||
plugin.RegisterRPCMethod("ocean-pay", "", "Ocean pay an offer for a miner payout", core.OceanPay)
|
||||
|
||||
plugin.Start()
|
||||
}
|
||||
|
|
1
go.mod
1
go.mod
|
@ -8,6 +8,7 @@ require (
|
|||
)
|
||||
|
||||
require (
|
||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||
github.com/vincenzopalazzo/cln4go v0.0.1-beta.8 // indirect
|
||||
github.com/vincenzopalazzo/cln4go/comm v0.0.0-20230413130716-35626bd3b34b // indirect
|
||||
github.com/vincenzopalazzo/cpstl/go v0.0.0-20221204131531-d0659db4dd2a // indirect
|
||||
|
|
2
go.sum
2
go.sum
|
@ -1,3 +1,5 @@
|
|||
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
|
||||
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
||||
github.com/vincenzopalazzo/cln4go v0.0.1-beta.8 h1:j5KAUwSehDa9HrFd5M6/kRD8NEiTc0CiClixl5e9gTk=
|
||||
github.com/vincenzopalazzo/cln4go v0.0.1-beta.8/go.mod h1:2NnveaBEhWeXc6SAyTLGIYRJsVq+5RjTRKh105b8Y2M=
|
||||
github.com/vincenzopalazzo/cln4go/client v0.0.0-20220802085837-fb816fecb890 h1:ylUf6ddD7TGHC+CXrAq0EQr5EsPVRYuiHDlPs1AIXf4=
|
||||
|
|
|
@ -1,17 +1,47 @@
|
|||
package plugin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
json "github.com/mitchellh/mapstructure"
|
||||
|
||||
"github.com/vincenzopalazzo/cln4go/client"
|
||||
"github.com/vincenzopalazzo/cln4go/plugin"
|
||||
)
|
||||
|
||||
type PluginState struct{}
|
||||
type State struct {
|
||||
rpc *client.UnixRPC
|
||||
}
|
||||
|
||||
func Hello(plugin *plugin.Plugin[*PluginState], request map[string]any) (map[string]any, error) {
|
||||
func (self *State) Rpc(method string, args map[string]any) (map[string]any, error) {
|
||||
return client.Call[map[string]any, map[string]any](self.rpc, method, args)
|
||||
}
|
||||
|
||||
func OceanPay(plugin *plugin.Plugin[*State], request map[string]any) (map[string]any, error) {
|
||||
return map[string]any{"message": "hello from cln4go.template"}, nil
|
||||
}
|
||||
|
||||
func OnShutdown(plugin *plugin.Plugin[*PluginState], request map[string]any) {
|
||||
func OnShutdown(plugin *plugin.Plugin[*State], request map[string]any) {
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
func OnInit(plugin *plugin.Plugin[*State], conf map[string]any) map[string]any {
|
||||
clnConf := struct {
|
||||
LightningDir string
|
||||
RpcFile string
|
||||
}{}
|
||||
if err := json.Decode(conf, &clnConf); err != nil {
|
||||
return map[string]any{
|
||||
"disable": err,
|
||||
}
|
||||
}
|
||||
rpc, err := client.NewUnix(fmt.Sprintf("%s/%s", clnConf.LightningDir, clnConf.RpcFile))
|
||||
if err != nil {
|
||||
return map[string]any{
|
||||
"disable": err,
|
||||
}
|
||||
}
|
||||
plugin.State.rpc = rpc
|
||||
return map[string]any{}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue