feat: 配置列表分页与钉钉机器人分钟级排队限流
统一 sources/templates/channels/rules 列表为分页响应,避免配置增多时全量返回;按钉钉 access_token 限制每分钟发送并在超限时等待下一分钟,降低触发官方封禁风险。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -47,12 +47,19 @@ func (h *ChannelHandler) Create(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (h *ChannelHandler) List(c *gin.Context) {
|
||||
channels, err := h.store.ListChannels(c.Request.Context())
|
||||
var page store.PageFilter
|
||||
if err := c.ShouldBindQuery(&page); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
page.Normalize()
|
||||
|
||||
channels, total, err := h.store.ListChannels(c.Request.Context(), page)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, channels)
|
||||
c.JSON(http.StatusOK, gin.H{"data": channels, "total": total, "page": page.Page})
|
||||
}
|
||||
|
||||
func (h *ChannelHandler) Get(c *gin.Context) {
|
||||
|
||||
@@ -22,6 +22,7 @@ func (h *MessageLogHandler) List(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
filter.Normalize()
|
||||
|
||||
logs, total, err := h.store.ListMessageLogs(c.Request.Context(), filter)
|
||||
if err != nil {
|
||||
|
||||
@@ -81,12 +81,19 @@ func (h *RuleHandler) Create(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (h *RuleHandler) List(c *gin.Context) {
|
||||
rules, err := h.store.ListRules(c.Request.Context())
|
||||
var page store.PageFilter
|
||||
if err := c.ShouldBindQuery(&page); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
page.Normalize()
|
||||
|
||||
rules, total, err := h.store.ListRules(c.Request.Context(), page)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, rules)
|
||||
c.JSON(http.StatusOK, gin.H{"data": rules, "total": total, "page": page.Page})
|
||||
}
|
||||
|
||||
func (h *RuleHandler) Get(c *gin.Context) {
|
||||
@@ -163,19 +170,35 @@ func (h *RuleHandler) Delete(c *gin.Context) {
|
||||
|
||||
func (h *RuleHandler) Enable(c *gin.Context) {
|
||||
id, _ := strconv.Atoi(c.Param("id"))
|
||||
rule, err := h.store.GetRule(c.Request.Context(), id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "rule not found"})
|
||||
return
|
||||
}
|
||||
if err := h.store.SetRuleEnabled(c.Request.Context(), id, true); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if h.cache != nil {
|
||||
_ = h.cache.InvalidateRule(c.Request.Context(), rule.SourceID, rule.Event)
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"ok": true})
|
||||
}
|
||||
|
||||
func (h *RuleHandler) Disable(c *gin.Context) {
|
||||
id, _ := strconv.Atoi(c.Param("id"))
|
||||
rule, err := h.store.GetRule(c.Request.Context(), id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "rule not found"})
|
||||
return
|
||||
}
|
||||
if err := h.store.SetRuleEnabled(c.Request.Context(), id, false); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if h.cache != nil {
|
||||
_ = h.cache.InvalidateRule(c.Request.Context(), rule.SourceID, rule.Event)
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"ok": true})
|
||||
}
|
||||
|
||||
@@ -186,6 +209,9 @@ func (h *RuleHandler) EnableChannel(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if h.cache != nil {
|
||||
_ = h.cache.InvalidateChannels(c.Request.Context(), ruleID)
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"ok": true})
|
||||
}
|
||||
|
||||
@@ -196,6 +222,9 @@ func (h *RuleHandler) DisableChannel(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if h.cache != nil {
|
||||
_ = h.cache.InvalidateChannels(c.Request.Context(), ruleID)
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"ok": true})
|
||||
}
|
||||
|
||||
|
||||
@@ -54,12 +54,19 @@ func (h *SourceHandler) Create(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (h *SourceHandler) List(c *gin.Context) {
|
||||
sources, err := h.store.ListSources(c.Request.Context())
|
||||
var page store.PageFilter
|
||||
if err := c.ShouldBindQuery(&page); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
page.Normalize()
|
||||
|
||||
sources, total, err := h.store.ListSources(c.Request.Context(), page)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, sources)
|
||||
c.JSON(http.StatusOK, gin.H{"data": sources, "total": total, "page": page.Page})
|
||||
}
|
||||
|
||||
func (h *SourceHandler) Get(c *gin.Context) {
|
||||
|
||||
@@ -40,12 +40,19 @@ func (h *TemplateHandler) Create(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (h *TemplateHandler) List(c *gin.Context) {
|
||||
templates, err := h.store.ListTemplates(c.Request.Context())
|
||||
var page store.PageFilter
|
||||
if err := c.ShouldBindQuery(&page); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
page.Normalize()
|
||||
|
||||
templates, total, err := h.store.ListTemplates(c.Request.Context(), page)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, templates)
|
||||
c.JSON(http.StatusOK, gin.H{"data": templates, "total": total, "page": page.Page})
|
||||
}
|
||||
|
||||
func (h *TemplateHandler) Get(c *gin.Context) {
|
||||
|
||||
Reference in New Issue
Block a user