From dcdc949a90e6c6bf9efdcbf3834a14dc3cb9f1a1 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Thu, 24 Aug 2023 15:45:23 +0800 Subject: [PATCH] fix --- constants/websocket.go | 6 +- ...otifyhandler.go => commonnotifyhandler.go} | 8 +- .../handler/registeraccountnotifyhandler.go | 35 -------- server/websocket/internal/handler/routes.go | 9 +- .../internal/logic/commonnotifylogic.go | 59 +++++++++++++ .../internal/logic/loginnotifylogic.go | 83 ------------------- .../logic/registeraccountnotifylogic.go | 83 ------------------- server/websocket/internal/types/types.go | 9 +- server_api/websocket.api | 20 ++--- utils/websocket_data/notify_data.go | 8 -- 10 files changed, 77 insertions(+), 243 deletions(-) rename server/websocket/internal/handler/{loginnotifyhandler.go => commonnotifyhandler.go} (73%) delete mode 100644 server/websocket/internal/handler/registeraccountnotifyhandler.go create mode 100644 server/websocket/internal/logic/commonnotifylogic.go delete mode 100644 server/websocket/internal/logic/loginnotifylogic.go delete mode 100644 server/websocket/internal/logic/registeraccountnotifylogic.go delete mode 100644 utils/websocket_data/notify_data.go diff --git a/constants/websocket.go b/constants/websocket.go index 98f4e61a..fc9b5c60 100644 --- a/constants/websocket.go +++ b/constants/websocket.go @@ -20,8 +20,6 @@ const ( WEBSOCKET_RENDER_IMAGE_ERR Websocket = "WEBSOCKET_RENDER_IMAGE_ERR" //传入数据格式错误 WEBSOCKET_ERR_DATA_FORMAT Websocket = "WEBSOCKET_ERR_DATA_FORMAT" - //登录回调通知 - WEBSOCKET_LOGIN_NOTIFY Websocket = "WEBSOCKET_LOGIN_NOTIFY" - //注册帐号回调通知 - WEBSOCKET_REGISTER_NOTIFY Websocket = "WEBSOCKET_REGISTER_NOTIFY" + //通用回调通知 + WEBSOCKET_COMMON_NOTIFY Websocket = "WEBSOCKET_COMMON_NOTIFY" ) diff --git a/server/websocket/internal/handler/loginnotifyhandler.go b/server/websocket/internal/handler/commonnotifyhandler.go similarity index 73% rename from server/websocket/internal/handler/loginnotifyhandler.go rename to server/websocket/internal/handler/commonnotifyhandler.go index 5ce5173a..ad63ed7f 100644 --- a/server/websocket/internal/handler/loginnotifyhandler.go +++ b/server/websocket/internal/handler/commonnotifyhandler.go @@ -11,22 +11,22 @@ import ( "fusenapi/server/websocket/internal/types" ) -func LoginNotifyHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { +func CommonNotifyHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - var req types.LoginNotifyReq + var req types.CommonNotifyReq userinfo, err := basic.RequestParse(w, r, svcCtx, &req) if err != nil { return } // 创建一个业务逻辑层实例 - l := logic.NewLoginNotifyLogic(r.Context(), svcCtx) + l := logic.NewCommonNotifyLogic(r.Context(), svcCtx) rl := reflect.ValueOf(l) basic.BeforeLogic(w, r, rl) - resp := l.LoginNotify(&req, userinfo) + resp := l.CommonNotify(&req, userinfo) if !basic.AfterLogic(w, r, rl, resp) { basic.NormalAfterLogic(w, r, resp) diff --git a/server/websocket/internal/handler/registeraccountnotifyhandler.go b/server/websocket/internal/handler/registeraccountnotifyhandler.go deleted file mode 100644 index 94c528d3..00000000 --- a/server/websocket/internal/handler/registeraccountnotifyhandler.go +++ /dev/null @@ -1,35 +0,0 @@ -package handler - -import ( - "net/http" - "reflect" - - "fusenapi/utils/basic" - - "fusenapi/server/websocket/internal/logic" - "fusenapi/server/websocket/internal/svc" - "fusenapi/server/websocket/internal/types" -) - -func RegisterAccountNotifyHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - - var req types.RegisterAccountNotifyReq - userinfo, err := basic.RequestParse(w, r, svcCtx, &req) - if err != nil { - return - } - - // 创建一个业务逻辑层实例 - l := logic.NewRegisterAccountNotifyLogic(r.Context(), svcCtx) - - rl := reflect.ValueOf(l) - basic.BeforeLogic(w, r, rl) - - resp := l.RegisterAccountNotify(&req, userinfo) - - if !basic.AfterLogic(w, r, rl, resp) { - basic.NormalAfterLogic(w, r, resp) - } - } -} diff --git a/server/websocket/internal/handler/routes.go b/server/websocket/internal/handler/routes.go index dc46d40b..8a2c5bd1 100644 --- a/server/websocket/internal/handler/routes.go +++ b/server/websocket/internal/handler/routes.go @@ -24,13 +24,8 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { }, { Method: http.MethodPost, - Path: "/api/websocket/register_account_notify", - Handler: RegisterAccountNotifyHandler(serverCtx), - }, - { - Method: http.MethodPost, - Path: "/api/websocket/login_notify", - Handler: LoginNotifyHandler(serverCtx), + Path: "/api/websocket/common_notify", + Handler: CommonNotifyHandler(serverCtx), }, }, ) diff --git a/server/websocket/internal/logic/commonnotifylogic.go b/server/websocket/internal/logic/commonnotifylogic.go new file mode 100644 index 00000000..88f25c6c --- /dev/null +++ b/server/websocket/internal/logic/commonnotifylogic.go @@ -0,0 +1,59 @@ +package logic + +import ( + "context" + "fusenapi/constants" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + + "fusenapi/server/websocket/internal/svc" + "fusenapi/server/websocket/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type CommonNotifyLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewCommonNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommonNotifyLogic { + return &CommonNotifyLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *CommonNotifyLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +func (l *CommonNotifyLogic) CommonNotify(req *types.CommonNotifyReq, userinfo *auth.UserInfo) (resp *basic.Response) { + if req.Data == "" { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "data is empty") + } + //websocket连接id不能为空 + if req.WebsocketId == "" { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "websocket connect id is empty") + } + //查询websocket连接 + value, ok := mapConnPool.Load(req.WebsocketId) + if !ok { + return resp.SetStatusWithMessage(basic.CodeOK, "success,but connection is not found") + } + //断言连接 + ws, ok := value.(wsConnectItem) + if !ok { + logx.Error("渲染回调断言websocket连接失败") + return resp.SetStatusWithMessage(basic.CodeServiceErr, "断言连接错误") + } + ws.sendToOutChan(ws.respondDataFormat(constants.WEBSOCKET_COMMON_NOTIFY, req.Data)) + return resp.SetStatusWithMessage(basic.CodeOK, "success") +} + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *CommonNotifyLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } diff --git a/server/websocket/internal/logic/loginnotifylogic.go b/server/websocket/internal/logic/loginnotifylogic.go deleted file mode 100644 index cf8a1298..00000000 --- a/server/websocket/internal/logic/loginnotifylogic.go +++ /dev/null @@ -1,83 +0,0 @@ -package logic - -//登录回调 -import ( - "encoding/json" - "fusenapi/constants" - "fusenapi/utils/auth" - "fusenapi/utils/basic" - "fusenapi/utils/encryption_decryption" - "fusenapi/utils/websocket_data" - "time" - - "context" - - "fusenapi/server/websocket/internal/svc" - "fusenapi/server/websocket/internal/types" - - "github.com/zeromicro/go-zero/core/logx" -) - -type LoginNotifyLogic struct { - logx.Logger - ctx context.Context - svcCtx *svc.ServiceContext -} - -func NewLoginNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginNotifyLogic { - return &LoginNotifyLogic{ - Logger: logx.WithContext(ctx), - ctx: ctx, - svcCtx: svcCtx, - } -} - -// 处理进入前逻辑w,r -// func (l *LoginNotifyLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { -// } - -func (l *LoginNotifyLogic) LoginNotify(req *types.LoginNotifyReq, userinfo *auth.UserInfo) (resp *basic.Response) { - if req.Data == "" { - return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "data is empty") - } - //解密数据 - data, err := encryption_decryption.CBCDecrypt(req.Data) - if err != nil { - logx.Error("解密失败:", err) - return resp.SetStatusWithMessage(basic.CodeServiceErr, "invalid data ") - } - //真正的数据结构 - var parseInfo websocket_data.NotifyData - if err = json.Unmarshal([]byte(data), &parseInfo); err != nil { - logx.Error("failed to parse json data:", err) - return resp.SetStatusWithMessage(basic.CodeServiceErr, "invalid format of parse data") - } - //websocket连接id不能为空 - if parseInfo.WebsocketConnectId == "" { - return resp.SetStatusWithMessage(basic.CodeServiceErr, "websocket connect id is empty") - } - now := time.Now().UTC().Unix() - //请求时间前后20秒都会失效 - if parseInfo.RequestTime < now-20 || parseInfo.RequestTime > now+20 { - return resp.SetStatusWithMessage(basic.CodeServiceErr, "invalid data ,time is not in allowed range") - } - //查询websocket连接 - value, ok := mapConnPool.Load(parseInfo.WebsocketConnectId) - if !ok { - return resp.SetStatusWithMessage(basic.CodeOK, "success,but connection is not found") - } - //断言连接 - ws, ok := value.(wsConnectItem) - if !ok { - logx.Error("渲染回调断言websocket连接失败") - return resp.SetStatusWithMessage(basic.CodeServiceErr, "断言连接错误") - } - //发送消息到出口缓冲池 - ws.sendToOutChan(ws.respondDataFormat(constants.WEBSOCKET_LOGIN_NOTIFY, parseInfo.Data)) - return resp.SetStatusWithMessage(basic.CodeOK, "success") -} - -// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 -// func (l *LoginNotifyLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { -// // httpx.OkJsonCtx(r.Context(), w, resp) -// } diff --git a/server/websocket/internal/logic/registeraccountnotifylogic.go b/server/websocket/internal/logic/registeraccountnotifylogic.go deleted file mode 100644 index 7b82dc00..00000000 --- a/server/websocket/internal/logic/registeraccountnotifylogic.go +++ /dev/null @@ -1,83 +0,0 @@ -package logic - -//注册帐号回调 -import ( - "encoding/json" - "fusenapi/constants" - "fusenapi/utils/auth" - "fusenapi/utils/basic" - "fusenapi/utils/encryption_decryption" - "fusenapi/utils/websocket_data" - "time" - - "context" - - "fusenapi/server/websocket/internal/svc" - "fusenapi/server/websocket/internal/types" - - "github.com/zeromicro/go-zero/core/logx" -) - -type RegisterAccountNotifyLogic struct { - logx.Logger - ctx context.Context - svcCtx *svc.ServiceContext -} - -func NewRegisterAccountNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterAccountNotifyLogic { - return &RegisterAccountNotifyLogic{ - Logger: logx.WithContext(ctx), - ctx: ctx, - svcCtx: svcCtx, - } -} - -// 处理进入前逻辑w,r -// func (l *RegisterAccountNotifyLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { -// } - -func (l *RegisterAccountNotifyLogic) RegisterAccountNotify(req *types.RegisterAccountNotifyReq, userinfo *auth.UserInfo) (resp *basic.Response) { - if req.Data == "" { - return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "data is empty") - } - //解密数据 - data, err := encryption_decryption.CBCDecrypt(req.Data) - if err != nil { - logx.Error("解密失败:", err) - return resp.SetStatusWithMessage(basic.CodeServiceErr, "invalid data ") - } - //真正的数据结构 - var parseInfo websocket_data.NotifyData - if err = json.Unmarshal([]byte(data), &parseInfo); err != nil { - logx.Error("failed to parse json data:", err) - return resp.SetStatusWithMessage(basic.CodeServiceErr, "invalid format of parse data") - } - //websocket连接id不能为空 - if parseInfo.WebsocketConnectId == "" { - return resp.SetStatusWithMessage(basic.CodeServiceErr, "websocket connect id is empty") - } - now := time.Now().UTC().Unix() - //请求时间前后20秒都会失效 - if parseInfo.RequestTime < now-20 || parseInfo.RequestTime > now+20 { - return resp.SetStatusWithMessage(basic.CodeServiceErr, "invalid data ,time is not in allowed range") - } - //查询websocket连接 - value, ok := mapConnPool.Load(parseInfo.WebsocketConnectId) - if !ok { - return resp.SetStatusWithMessage(basic.CodeOK, "success,but connection is not found") - } - //断言连接 - ws, ok := value.(wsConnectItem) - if !ok { - logx.Error("渲染回调断言websocket连接失败") - return resp.SetStatusWithMessage(basic.CodeServiceErr, "断言连接错误") - } - //发送消息到出口缓冲池 - ws.sendToOutChan(ws.respondDataFormat(constants.WEBSOCKET_REGISTER_NOTIFY, parseInfo.Data)) - return resp.SetStatusWithMessage(basic.CodeOK, "success") -} - -// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 -// func (l *RegisterAccountNotifyLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { -// // httpx.OkJsonCtx(r.Context(), w, resp) -// } diff --git a/server/websocket/internal/types/types.go b/server/websocket/internal/types/types.go index d409d63a..384b456e 100644 --- a/server/websocket/internal/types/types.go +++ b/server/websocket/internal/types/types.go @@ -12,12 +12,9 @@ type RenderNotifyReq struct { Image string `json:"image"` } -type RegisterAccountNotifyReq struct { - Data string `json:"data"` //aes_cbc加密密文 -} - -type LoginNotifyReq struct { - Data string `json:"data"` //aes_cbc加密密文 +type CommonNotifyReq struct { + WebsocketId string `json:"websocket_id"` //websocket连接标识 + Data string `json:"data"` //aes_cbc加密密文 } type Request struct { diff --git a/server_api/websocket.api b/server_api/websocket.api index c37ec39a..07c52943 100644 --- a/server_api/websocket.api +++ b/server_api/websocket.api @@ -15,12 +15,9 @@ service websocket { //云渲染完了通知接口 @handler RenderNotifyHandler post /api/websocket/render_notify(RenderNotifyReq) returns (response); - //注册回调 - @handler RegisterAccountNotifyHandler - post /api/websocket/register_account_notify(RegisterAccountNotifyReq) returns (response); - //登录回调 - @handler LoginNotifyHandler - post /api/websocket/login_notify(LoginNotifyReq) returns (response); + //通用回调接口 + @handler CommonNotifyHandler + post /api/websocket/common_notify(CommonNotifyReq) returns (response); } //渲染完了通知接口 @@ -30,11 +27,8 @@ type RenderNotifyReq { GuestId int64 `json:"guest_id"` Image string `json:"image"` } -//注册回调 -type RegisterAccountNotifyReq { - Data string `json:"data"` //aes_cbc加密密文 -} -//登录回调 -type LoginNotifyReq { - Data string `json:"data"` //aes_cbc加密密文 +//通用回调接口 +type CommonNotifyReq { + WebsocketId string `json:"websocket_id"` //websocket连接标识 + Data string `json:"data"` //aes_cbc加密密文 } \ No newline at end of file diff --git a/utils/websocket_data/notify_data.go b/utils/websocket_data/notify_data.go deleted file mode 100644 index 67c627ba..00000000 --- a/utils/websocket_data/notify_data.go +++ /dev/null @@ -1,8 +0,0 @@ -package websocket_data - -// 请求回调接口数据(登录|注册) -type NotifyData struct { - WebsocketConnectId string `json:"websocket_connect_id"` //websocket连接唯一标识 - RequestTime int64 `json:"request_time"` //请求回调时的utc时间 - Data interface{} `json:"data"` //其他数据 -}