handler.go
1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package goqueue
import (
rawjson "encoding/json"
"fmt"
"github.com/linmadan/egglib-go/utils/json"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/blockChain/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/blockChain/service"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/log"
)
func UpToChainHandler(k, v string) error {
blockChainService := service.NewBlockChainService(nil)
upChainCommand := &command.UpChainCommand{}
err := json.UnmarshalFromString(v, upChainCommand)
if err != nil {
log.Logger.Error(err.Error(), map[string]interface{}{"info": "UpToChainHandler 解析json错误", "data": v})
return err
}
_, err = blockChainService.UpChain(upChainCommand)
return err
}
func FilterTopicHandler(topic string, handler func(string, string) error) func(string, string) error {
return func(k string, v string) error {
raw := &RawData{}
err := json.UnmarshalFromString(v, raw)
if err != nil {
log.Logger.Error("【GoQueue】消息解析错误:"+err.Error(), map[string]interface{}{"info": "FilterTopicHandler 解析json错误", "data": v})
return err
}
log.Logger.Debug(fmt.Sprintf("【GoQueue】收到消息 Topic:%s", raw.Topic), map[string]interface{}{"data": v})
if raw.Topic != topic {
log.Logger.Debug(fmt.Sprintf("【GoQueue】 topic not equal get:%v want:%v", raw.Topic, topic), map[string]interface{}{"data": v})
return nil
}
return handler(k, string(raw.Data))
}
}
type RawData struct {
Topic string `json:"topic"`
Data rawjson.RawMessage `json:"data"`
}