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--
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)