-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessageController.go
91 lines (85 loc) · 2.64 KB
/
MessageController.go
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package controller
import (
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/maczh/mgin/client"
"github.com/maczh/mgin/logs"
"github.com/maczh/mgin/models"
"github.com/maczh/mgrmq/model"
"github.com/maczh/mgrmq/service"
)
var msgService *service.MessageService
func Init() {
msgService = service.NewMessageService()
}
// SendMessage godoc
// @Summary 发送一条消息到消息队列
// @Description 发送一条消息到消息队列
// @Tags 发布
// @Accept x-www-form-urlencoded
// @Produce json
// @Param queue formData string true "消息队列名"
// @Param msg formData string true "消息内容,必须是mgin中的client.Options格式或map格式,在yml文件中配置paramType"
// @Success 200 {string} string "ok"
// @Router /msg/send [post]
func SendMessage(c *gin.Context) models.Result[any] {
var req model.SendReq
var err error
switch c.ContentType() {
case gin.MIMEJSON:
err = c.ShouldBindJSON(&req)
default:
err = c.ShouldBind(&req)
}
if err != nil {
logs.Error("绑定入参错误:{}", err.Error())
return models.Error(-1, "入参模式错误")
}
queue, msg := req.Queue, req.Msg
if queue == "" {
return models.Error(-1, "消息队列名称不可为空")
}
if msg == "" {
return models.Error(-1, "消息内容不可为空")
}
if service.ParamsType == "map" {
m := map[string]string{}
err = json.Unmarshal([]byte(msg), &m)
if err != nil {
return models.Error(-1, "消息格式不正确,不是map格式")
}
} else {
var msgBody client.Options
err = json.Unmarshal([]byte(msg), &msgBody)
if err != nil || msgBody.Method == "" {
return models.Error(-1, "消息内容不正确")
}
}
return msgService.Send(queue, msg)
}
// ReSendFailedMessage godoc
// @Summary 重新发送指定时间段内的失败消息
// @Description 重新发送指定时间段内的失败消息
// @Tags 发布
// @Accept x-www-form-urlencoded
// @Produce json
// @Param queue formData string false "消息队列名,不传为配置中的所有队列"
// @Param start formData string false "开始时间,格式 yyyy-MM-dd HH:mm:ss,不传默认为当天0点"
// @Param end formData string false "结束时间,格式 yyyy-MM-dd HH:mm:ss,不传默认为当前时间"
// @Success 200 {string} string "ok"
// @Router /msg/resend [post]
func ReSendFailedMessage(c *gin.Context) models.Result[any] {
var req model.ReSendReq
var err error
switch c.ContentType() {
case gin.MIMEJSON:
err = c.ShouldBindJSON(&req)
default:
err = c.ShouldBind(&req)
}
if err != nil {
logs.Error("绑定入参错误:{}", err.Error())
return models.Error(-1, "入参模式错误")
}
return msgService.ReSend(req.Queue, req.Start, req.End)
}