fusenapi/server/websocket/internal/handler/rendernotifyhandler.go

106 lines
2.7 KiB
Go
Raw Normal View History

2023-07-25 09:10:50 +00:00
package handler
import (
2023-07-25 09:41:38 +00:00
"crypto/sha256"
"encoding/hex"
2023-07-25 09:10:50 +00:00
"encoding/json"
2023-07-25 09:41:38 +00:00
"fmt"
"fusenapi/constants"
2023-07-25 09:10:50 +00:00
"fusenapi/utils/basic"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"net/http"
2023-07-25 09:41:38 +00:00
"time"
2023-07-25 09:10:50 +00:00
"fusenapi/server/websocket/internal/svc"
"fusenapi/server/websocket/internal/types"
)
func RenderNotifyHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.RenderNotifyReq
_, err := basic.RequestParse(w, r, svcCtx, &req)
if err != nil {
2023-07-25 09:41:38 +00:00
httpx.OkJsonCtx(r.Context(), w, basic.Response{
Code: basic.CodeRequestParamsErr.Code,
Message: "err param",
Data: nil,
})
2023-07-25 09:10:50 +00:00
return
}
2023-07-25 09:41:38 +00:00
if len(req.NotifyList) == 0 {
httpx.OkJsonCtx(r.Context(), w, basic.Response{
Code: basic.CodeRequestParamsErr.Code,
Message: "invalid param,notify list is empty",
Data: nil,
})
return
}
2023-07-25 10:34:56 +00:00
if time.Now().Unix()-120 > req.Time /*|| req.Time > time.Now().Unix() */ {
2023-07-25 09:41:38 +00:00
httpx.OkJsonCtx(r.Context(), w, basic.Response{
Code: basic.CodeRequestParamsErr.Code,
Message: "invalid param,time is expired",
Data: nil,
})
return
}
//验证签名 sha256
notifyByte, _ := json.Marshal(req.NotifyList)
h := sha256.New()
h.Write([]byte(fmt.Sprintf(constants.RENDER_NOTIFY_SIGN_KEY, string(notifyByte), req.Time)))
signHex := h.Sum(nil)
sign := hex.EncodeToString(signHex)
if req.Sign != sign {
httpx.OkJsonCtx(r.Context(), w, basic.Response{
Code: basic.CodeRequestParamsErr.Code,
Message: "invalid sign",
Data: nil,
})
return
}
//遍历链接
2023-07-25 09:10:50 +00:00
mapConnPool.Range(func(key, value any) bool {
ws, ok := value.(wsConnectItem)
if !ok {
return false
}
setOutRenderImage(req, ws)
return true
})
httpx.OkJsonCtx(r.Context(), w, basic.Response{
Code: 200,
Message: "success",
Data: nil,
})
}
}
// 把渲染好的数据放入outchan
func setOutRenderImage(req types.RenderNotifyReq, ws wsConnectItem) {
ws.mutex.Lock()
defer ws.mutex.Unlock()
for _, notifyItem := range req.NotifyList {
2023-07-25 09:12:37 +00:00
renderKey := ws.getRenderImageMapKey(notifyItem.ProductId, notifyItem.SizeId, notifyItem.TemplateId)
2023-07-25 09:41:38 +00:00
//查询
2023-07-25 09:10:50 +00:00
_, ok := ws.renderImage[renderKey]
if !ok {
continue
}
responseData := types.RenderImageRspMsg{
ProductId: notifyItem.ProductId,
SizeId: notifyItem.SizeId,
TemplateId: notifyItem.TemplateId,
Source: "我是渲染资源",
}
b, _ := json.Marshal(responseData)
select {
case <-ws.closeChan:
return
case ws.outChan <- b:
logx.Info("notify send render result to out chan")
}
2023-07-25 09:41:38 +00:00
//删掉已经处理的渲染任务
delete(ws.renderImage, renderKey)
2023-07-25 09:10:50 +00:00
}
}