39f3774940
统一 sources/templates/channels/rules 列表为分页响应,避免配置增多时全量返回;按钉钉 access_token 限制每分钟发送并在超限时等待下一分钟,降低触发官方封禁风险。 Co-authored-by: Cursor <cursoragent@cursor.com>
117 lines
2.9 KiB
Go
117 lines
2.9 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"aiaa-notification-service/internal/cache"
|
|
"aiaa-notification-service/internal/model"
|
|
"aiaa-notification-service/internal/store"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type SourceHandler struct {
|
|
store *store.Store
|
|
cache *cache.Cache
|
|
}
|
|
|
|
func NewSourceHandler(s *store.Store, c *cache.Cache) *SourceHandler {
|
|
return &SourceHandler{store: s, cache: c}
|
|
}
|
|
|
|
type createSourceReq struct {
|
|
Name string `json:"name" binding:"required"`
|
|
ParseMode string `json:"parse_mode"`
|
|
ParsePattern string `json:"parse_pattern"`
|
|
Status int `json:"status"`
|
|
}
|
|
|
|
func (h *SourceHandler) Create(c *gin.Context) {
|
|
var req createSourceReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
if req.ParseMode == "" {
|
|
req.ParseMode = "json"
|
|
}
|
|
if req.Status == 0 {
|
|
req.Status = 1
|
|
}
|
|
|
|
src := &model.Source{
|
|
Name: req.Name,
|
|
ParseMode: req.ParseMode,
|
|
ParsePattern: req.ParsePattern,
|
|
Status: req.Status,
|
|
}
|
|
if err := h.store.CreateSource(c.Request.Context(), src); err != nil {
|
|
c.JSON(http.StatusConflict, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusCreated, src)
|
|
}
|
|
|
|
func (h *SourceHandler) List(c *gin.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, gin.H{"data": sources, "total": total, "page": page.Page})
|
|
}
|
|
|
|
func (h *SourceHandler) Get(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
src, err := h.store.GetSource(c.Request.Context(), id)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "source not found"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, src)
|
|
}
|
|
|
|
func (h *SourceHandler) Update(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
var req createSourceReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
src := &model.Source{
|
|
Name: req.Name,
|
|
ParseMode: req.ParseMode,
|
|
ParsePattern: req.ParsePattern,
|
|
Status: req.Status,
|
|
}
|
|
if err := h.store.UpdateSource(c.Request.Context(), id, src); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
// Invalidate cache
|
|
if h.cache != nil {
|
|
_ = h.cache.InvalidateBySource(c.Request.Context(), id)
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"ok": true})
|
|
}
|
|
|
|
func (h *SourceHandler) Delete(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
if err := h.store.DeleteSource(c.Request.Context(), id); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"ok": true})
|
|
}
|