notification.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package handler
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. "spider/internal/model"
  6. "spider/internal/notification"
  7. "spider/internal/store"
  8. "github.com/gin-gonic/gin"
  9. "gorm.io/datatypes"
  10. )
  11. // NotificationHandler handles notification config CRUD.
  12. type NotificationHandler struct {
  13. store *store.Store
  14. manager *notification.Manager
  15. }
  16. // List handles GET /notification-configs
  17. func (h *NotificationHandler) List(c *gin.Context) {
  18. var configs []model.NotificationConfig
  19. if err := h.store.DB.Order("created_at DESC").Find(&configs).Error; err != nil {
  20. Fail(c, 500, err.Error())
  21. return
  22. }
  23. OK(c, configs)
  24. }
  25. // Create handles POST /notification-configs
  26. func (h *NotificationHandler) Create(c *gin.Context) {
  27. var body struct {
  28. Name string `json:"name" binding:"required"`
  29. EventType string `json:"event_type" binding:"required"`
  30. Channel string `json:"channel" binding:"required"`
  31. Config map[string]string `json:"config"`
  32. Enabled bool `json:"enabled"`
  33. }
  34. if err := c.ShouldBindJSON(&body); err != nil {
  35. Fail(c, 400, err.Error())
  36. return
  37. }
  38. configJSON, _ := json.Marshal(body.Config)
  39. cfg := model.NotificationConfig{
  40. Name: body.Name,
  41. EventType: body.EventType,
  42. Channel: body.Channel,
  43. Config: datatypes.JSON(configJSON),
  44. Enabled: body.Enabled,
  45. }
  46. if err := h.store.DB.Create(&cfg).Error; err != nil {
  47. Fail(c, 500, err.Error())
  48. return
  49. }
  50. LogAudit(h.store, c, "create", "notification", strconv.Itoa(int(cfg.ID)), gin.H{"name": cfg.Name})
  51. OK(c, cfg)
  52. }
  53. // Update handles PUT /notification-configs/:id
  54. func (h *NotificationHandler) Update(c *gin.Context) {
  55. id, err := strconv.ParseUint(c.Param("id"), 10, 64)
  56. if err != nil {
  57. Fail(c, 400, "invalid id")
  58. return
  59. }
  60. var existing model.NotificationConfig
  61. if err := h.store.DB.First(&existing, id).Error; err != nil {
  62. Fail(c, 404, "not found")
  63. return
  64. }
  65. var body struct {
  66. Name *string `json:"name"`
  67. EventType *string `json:"event_type"`
  68. Channel *string `json:"channel"`
  69. Config *map[string]string `json:"config"`
  70. Enabled *bool `json:"enabled"`
  71. }
  72. if err := c.ShouldBindJSON(&body); err != nil {
  73. Fail(c, 400, err.Error())
  74. return
  75. }
  76. updates := map[string]any{}
  77. if body.Name != nil {
  78. updates["name"] = *body.Name
  79. }
  80. if body.EventType != nil {
  81. updates["event_type"] = *body.EventType
  82. }
  83. if body.Channel != nil {
  84. updates["channel"] = *body.Channel
  85. }
  86. if body.Config != nil {
  87. configJSON, _ := json.Marshal(*body.Config)
  88. updates["config"] = datatypes.JSON(configJSON)
  89. }
  90. if body.Enabled != nil {
  91. updates["enabled"] = *body.Enabled
  92. }
  93. h.store.DB.Model(&existing).Updates(updates)
  94. h.store.DB.First(&existing, id)
  95. OK(c, existing)
  96. }
  97. // Delete handles DELETE /notification-configs/:id
  98. func (h *NotificationHandler) Delete(c *gin.Context) {
  99. id, err := strconv.ParseUint(c.Param("id"), 10, 64)
  100. if err != nil {
  101. Fail(c, 400, "invalid id")
  102. return
  103. }
  104. if err := h.store.DB.Delete(&model.NotificationConfig{}, id).Error; err != nil {
  105. Fail(c, 500, err.Error())
  106. return
  107. }
  108. OK(c, gin.H{"message": "已删除"})
  109. }
  110. // Test handles POST /notification-configs/:id/test
  111. func (h *NotificationHandler) Test(c *gin.Context) {
  112. id, err := strconv.ParseUint(c.Param("id"), 10, 64)
  113. if err != nil {
  114. Fail(c, 400, "invalid id")
  115. return
  116. }
  117. var cfg model.NotificationConfig
  118. if err := h.store.DB.First(&cfg, id).Error; err != nil {
  119. Fail(c, 404, "not found")
  120. return
  121. }
  122. if err := h.manager.SendTest(cfg); err != nil {
  123. Fail(c, 500, err.Error())
  124. return
  125. }
  126. OK(c, gin.H{"message": "测试通知已发送"})
  127. }