im 管理后台
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
// Package tcpclient
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package tcpclient
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"hotgo/api/servmsg"
|
||||
"hotgo/internal/library/network/tcp"
|
||||
"hotgo/internal/model/input/servmsgin"
|
||||
"hotgo/internal/service"
|
||||
"hotgo/utility/simple"
|
||||
)
|
||||
|
||||
// tcp客户端
|
||||
type sAuthClient struct {
|
||||
client *tcp.Client
|
||||
summary *servmsgin.AuthSummaryModel
|
||||
}
|
||||
|
||||
func init() {
|
||||
service.RegisterAuthClient(newAuthClient())
|
||||
}
|
||||
|
||||
func newAuthClient() *sAuthClient {
|
||||
return &sAuthClient{}
|
||||
}
|
||||
|
||||
// Instance 获取实例
|
||||
func (s *sAuthClient) Instance() *tcp.Client {
|
||||
return s.client
|
||||
}
|
||||
|
||||
// Start 启动服务
|
||||
func (s *sAuthClient) Start(ctx context.Context) {
|
||||
g.Log().Debug(ctx, "AuthClient start..")
|
||||
|
||||
config, err := service.SysConfig().GetLoadTCP(ctx)
|
||||
if err != nil {
|
||||
g.Log().Errorf(ctx, "AuthClient start fail:%+v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if config.Client == nil || config.Client.Auth == nil {
|
||||
g.Log().Errorf(ctx, "AuthClient config is invalid")
|
||||
return
|
||||
}
|
||||
|
||||
// 创建客户端配置
|
||||
clientConfig := &tcp.ClientConfig{
|
||||
Addr: config.Client.Auth.Address,
|
||||
AutoReconnect: true,
|
||||
Auth: &tcp.AuthMeta{
|
||||
Name: config.Client.Auth.Name,
|
||||
Extra: g.Map{
|
||||
"test": 13,
|
||||
},
|
||||
Group: config.Client.Auth.Group,
|
||||
AppId: config.Client.Auth.AppId,
|
||||
SecretKey: config.Client.Auth.SecretKey,
|
||||
},
|
||||
LoginEvent: s.onLoginEvent,
|
||||
CloseEvent: s.onCloseEvent,
|
||||
}
|
||||
|
||||
simple.SafeGo(ctx, func(ctx context.Context) {
|
||||
s.client = tcp.NewClient(clientConfig)
|
||||
|
||||
// 注册路由
|
||||
s.client.RegisterRouter(
|
||||
s.OnResponseAuthSummary, // 响应授权信息
|
||||
s.OnResponseExampleHello, // 一个tcp请求例子
|
||||
)
|
||||
|
||||
if err = s.client.Start(); err != nil {
|
||||
g.Log().Errorf(ctx, "AuthClient Start fail:%+v", err)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Stop 停止服务
|
||||
func (s *sAuthClient) Stop(ctx context.Context) {
|
||||
if s.client != nil && !s.client.IsStop() {
|
||||
s.client.Stop()
|
||||
g.Log().Debug(ctx, "AuthClient stop..")
|
||||
}
|
||||
}
|
||||
|
||||
// onLoginEvent 登录认证成功事件
|
||||
func (s *sAuthClient) onLoginEvent() {
|
||||
ctx := gctx.New()
|
||||
|
||||
// 获取授权信息
|
||||
_ = s.client.Send(ctx, &servmsg.AuthSummaryReq{})
|
||||
|
||||
// 测试例子,实际使用时可以注释掉
|
||||
s.testExample(ctx)
|
||||
|
||||
g.Log().Debug(ctx, "AuthClient login succeed.")
|
||||
}
|
||||
|
||||
// onCloseEvent 连接关闭回调事件
|
||||
func (s *sAuthClient) onCloseEvent() {
|
||||
g.Log().Debug(gctx.New(), "AuthClient closed.")
|
||||
}
|
||||
|
||||
// Summary 获取授权信息
|
||||
func (s *sAuthClient) Summary() *servmsgin.AuthSummaryModel {
|
||||
if s.summary == nil {
|
||||
s.summary = new(servmsgin.AuthSummaryModel)
|
||||
}
|
||||
return s.summary
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
// Package tcpclient
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package tcpclient
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"hotgo/api/servmsg"
|
||||
)
|
||||
|
||||
// OnResponseAuthSummary 响应授权信息
|
||||
func (s *sAuthClient) OnResponseAuthSummary(ctx context.Context, req *servmsg.AuthSummaryRes) {
|
||||
if err := req.GetError(); err != nil {
|
||||
g.Log().Warningf(ctx, "OnResponseAuthSummary GetError:%+v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 拿到授权的数据,可以是一些动态的功能、路由、权限控制等
|
||||
s.summary = req.Data
|
||||
}
|
||||
|
||||
// OnResponseExampleHello 一个tcp请求例子
|
||||
func (s *sAuthClient) OnResponseExampleHello(ctx context.Context, req *servmsg.ExampleHelloRes) {
|
||||
if err := req.GetError(); err != nil {
|
||||
g.Log().Warningf(ctx, "OnResponseExampleHello GetError:%+v", err)
|
||||
return
|
||||
}
|
||||
|
||||
g.Log().Infof(ctx, "OnResponseExampleHello data:%+v", req.Data)
|
||||
}
|
||||
|
||||
// testExample 测试例子
|
||||
func (s *sAuthClient) testExample(ctx context.Context) {
|
||||
// 发起tcp请求
|
||||
// 异步执行,服务端返回消息后会转到`OnResponseExampleHello`中
|
||||
_ = s.client.Send(ctx, &servmsg.ExampleHelloReq{
|
||||
Name: "Tom",
|
||||
})
|
||||
|
||||
// 发起rpc请求
|
||||
// 同步执行,阻塞等待服务端返回消息
|
||||
var req = &servmsg.ExampleRPCHelloReq{
|
||||
Name: "Tony",
|
||||
}
|
||||
var res *servmsg.ExampleRPCHelloRes
|
||||
if err := s.client.RequestScan(ctx, req, &res); err != nil {
|
||||
g.Log().Warningf(ctx, "client.Request ExampleRPCHelloReq err:%+v", err)
|
||||
return
|
||||
}
|
||||
g.Log().Infof(ctx, "ExampleRPCHelloRes data:%+v", res.Data)
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// Package tcpclient
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package tcpclient
|
||||
@@ -0,0 +1,102 @@
|
||||
// Package tcpclient
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package tcpclient
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"hotgo/internal/library/network/tcp"
|
||||
"hotgo/internal/service"
|
||||
"hotgo/utility/simple"
|
||||
)
|
||||
|
||||
// tcp客户端
|
||||
type sCronClient struct {
|
||||
client *tcp.Client
|
||||
}
|
||||
|
||||
func init() {
|
||||
service.RegisterCronClient(newCronClient())
|
||||
}
|
||||
|
||||
func newCronClient() *sCronClient {
|
||||
return &sCronClient{}
|
||||
}
|
||||
|
||||
// Instance 获取实例
|
||||
func (s *sCronClient) Instance() *tcp.Client {
|
||||
return s.client
|
||||
}
|
||||
|
||||
// Start 启动服务
|
||||
func (s *sCronClient) Start(ctx context.Context) {
|
||||
g.Log().Debug(ctx, "CronClient start..")
|
||||
|
||||
config, err := service.SysConfig().GetLoadTCP(ctx)
|
||||
if err != nil {
|
||||
g.Log().Errorf(ctx, "CronClient start fail:%+v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if config == nil || config.Client == nil || config.Client.Cron == nil {
|
||||
g.Log().Errorf(ctx, "CronClient config is invalid")
|
||||
return
|
||||
}
|
||||
|
||||
// 创建客户端配置
|
||||
clientConfig := &tcp.ClientConfig{
|
||||
Addr: config.Client.Cron.Address,
|
||||
AutoReconnect: true,
|
||||
Auth: &tcp.AuthMeta{
|
||||
Name: config.Client.Cron.Name,
|
||||
Group: config.Client.Cron.Group,
|
||||
AppId: config.Client.Cron.AppId,
|
||||
SecretKey: config.Client.Cron.SecretKey,
|
||||
},
|
||||
LoginEvent: s.onLoginEvent,
|
||||
CloseEvent: s.onCloseEvent,
|
||||
}
|
||||
|
||||
simple.SafeGo(ctx, func(ctx context.Context) {
|
||||
s.client = tcp.NewClient(clientConfig)
|
||||
|
||||
// 注册RPC路由
|
||||
s.client.RegisterRPCRouter(
|
||||
s.OnCronDelete, // 删除任务
|
||||
s.OnCronEdit, // 编辑任务
|
||||
s.OnCronStatus, // 修改任务状态
|
||||
s.OnCronOnlineExec, // 执行一次任务
|
||||
s.OnCronDispatchLog, // 查看调度日志
|
||||
)
|
||||
|
||||
// 注册拦截器
|
||||
s.client.RegisterInterceptor(s.DefaultInterceptor)
|
||||
|
||||
if err = s.client.Start(); err != nil {
|
||||
g.Log().Errorf(ctx, "CronClient Start fail:%+v", err)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Stop 停止服务
|
||||
func (s *sCronClient) Stop(ctx context.Context) {
|
||||
if s.client != nil && !s.client.IsStop() {
|
||||
s.client.Stop()
|
||||
g.Log().Debug(ctx, "CronClient stop..")
|
||||
}
|
||||
}
|
||||
|
||||
// onLoginEvent 登录认证成功事件
|
||||
func (s *sCronClient) onLoginEvent() {
|
||||
g.Log().Debug(gctx.New(), "CronClient login succeed.")
|
||||
}
|
||||
|
||||
// onCloseEvent 连接关闭回调事件
|
||||
func (s *sCronClient) onCloseEvent() {
|
||||
g.Log().Debug(gctx.New(), "CronClient closed.")
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
// Package tcpclient
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package tcpclient
|
||||
|
||||
import (
|
||||
"context"
|
||||
"hotgo/api/servmsg"
|
||||
"hotgo/internal/service"
|
||||
)
|
||||
|
||||
// OnCronDelete 删除任务
|
||||
func (s *sCronClient) OnCronDelete(ctx context.Context, req *servmsg.CronDeleteReq) (res *servmsg.CronDeleteRes, err error) {
|
||||
err = service.SysCron().Delete(ctx, req.CronDeleteInp)
|
||||
return
|
||||
}
|
||||
|
||||
// OnCronEdit 编辑任务
|
||||
func (s *sCronClient) OnCronEdit(ctx context.Context, req *servmsg.CronEditReq) (res *servmsg.CronEditRes, err error) {
|
||||
err = service.SysCron().Edit(ctx, req.CronEditInp)
|
||||
return
|
||||
}
|
||||
|
||||
// OnCronStatus 修改任务状态
|
||||
func (s *sCronClient) OnCronStatus(ctx context.Context, req *servmsg.CronStatusReq) (res *servmsg.CronStatusRes, err error) {
|
||||
err = service.SysCron().Status(ctx, req.CronStatusInp)
|
||||
return
|
||||
}
|
||||
|
||||
// OnCronOnlineExec 执行一次任务
|
||||
func (s *sCronClient) OnCronOnlineExec(ctx context.Context, req *servmsg.CronOnlineExecReq) (res *servmsg.CronOnlineExecRes, err error) {
|
||||
err = service.SysCron().OnlineExec(ctx, req.OnlineExecInp)
|
||||
return
|
||||
}
|
||||
|
||||
// OnCronDispatchLog 查看调度日志
|
||||
func (s *sCronClient) OnCronDispatchLog(ctx context.Context, req *servmsg.CronDispatchLogReq) (res *servmsg.CronDispatchLogRes, err error) {
|
||||
res = new(servmsg.CronDispatchLogRes)
|
||||
res.DispatchLogModel, err = service.SysCron().DispatchLog(ctx, req.DispatchLogInp)
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// Package tcpclient
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package tcpclient
|
||||
|
||||
import (
|
||||
"context"
|
||||
"hotgo/internal/library/network/tcp"
|
||||
)
|
||||
|
||||
// DefaultInterceptor 默认拦截器
|
||||
func (s *sCronClient) DefaultInterceptor(ctx context.Context, msg *tcp.Message) (err error) {
|
||||
//conn := tcp.ConnFromCtx(ctx)
|
||||
//g.Log().Warningf(ctx, "DefaultInterceptor msg:%+v, conn:%+v", msg, gjson.New(conn).String())
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user