gmqctl gen plugin生成样例代码

==官方文档==

https://github.com/DrmagicE/gmqtt/blob/master/plugin/README.md

 

==安装命令==

cd至gmqtt根目录:cd D:\work\10_Git\golang\src\gmqtt

执行命令:go install ./cmd/gmqctl

安装过程可能遇到文件夹Access is denied错误。

解决办法参考博客:https://www.cnblogs.com/quchunhui/p/16461171.html

 

==验证安装结果==

执行命令:gmqctl gen plugin --help

能够正确执行help命令就没有问题。

 

==生成源码文件==

命令样例:

gmqctl gen plugin -c -n storage -o D:\work\10_Git\golang -H OnSubscribe,OnSubscribed,OnUnsubscribe,OnUnsubscribed,OnMsgArrived

-c 指定之后会生成config.go

-n 插件的名字

-o 生成代码的存放路径

-H 需要生成哪些钩子函数

 

上述样例命令执行之后,正确生成了代码

 

==生成文件内容==

1. config.go

package storage

// Config is the configuration for the storage plugin.
type Config struct {
    // add your config fields
}

// Validate validates the configuration, and return an error if it is invalid.
func (c *Config) Validate() error {
    panic("implement me")
}

// DefaultConfig is the default configuration.
var DefaultConfig = Config{}

func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
    panic("implement me")
}

 

2.hooks.go

package storage

import (
    "github.com/DrmagicE/gmqtt/server"
)

func (s *Storage) HookWrapper() server.HookWrapper {
    return server.HookWrapper{
        OnSubscribeWrapper:    s.OnSubscribeWrapper,
        OnSubscribedWrapper:   s.OnSubscribedWrapper,
        OnUnsubscribeWrapper:  s.OnUnsubscribeWrapper,
        OnUnsubscribedWrapper: s.OnUnsubscribedWrapper,
        OnMsgArrivedWrapper:   s.OnMsgArrivedWrapper,
    }
}

func (s *Storage) OnSubscribeWrapper(pre server.OnSubscribe) server.OnSubscribe {
    panic("impermanent me")
}

func (s *Storage) OnSubscribedWrapper(pre server.OnSubscribed) server.OnSubscribed {
    panic("impermanent me")
}

func (s *Storage) OnUnsubscribeWrapper(pre server.OnUnsubscribe) server.OnUnsubscribe {
    panic("impermanent me")
}

func (s *Storage) OnUnsubscribedWrapper(pre server.OnUnsubscribed) server.OnUnsubscribed {
    panic("impermanent me")
}

func (s *Storage) OnMsgArrivedWrapper(pre server.OnMsgArrived) server.OnMsgArrived {
    panic("impermanent me")
}

 

3.storage.go

package storage

import (
    "go.uber.org/zap"

    "github.com/DrmagicE/gmqtt/config"
    "github.com/DrmagicE/gmqtt/server"
)

var _ server.Plugin = (*Storage)(nil)

const Name = "storage"

func init() {
    server.RegisterPlugin(Name, New)
    config.RegisterDefaultPluginConfig(Name, &DefaultConfig)
}

func New(config config.Config) (server.Plugin, error) {
    panic("implement me")
}

var log *zap.Logger

type Storage struct {
}

func (s *Storage) Load(service server.Server) error {
    log = server.LoggerWithField(zap.String("plugin", Name))
    panic("implement me")
}

func (s *Storage) Unload() error {
    panic("implement me")
}

func (s *Storage) Name() string {
    return Name
}

 

--End--

 

posted @ 2022-07-09 16:41  大墨垂杨  阅读(31)  评论(0编辑  收藏  举报