feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
2026-04-08 20:06:54 +08:00
|
|
|
"strconv"
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
2026-05-08 12:40:36 +08:00
|
|
|
"github.com/user-management-system/internal/pagination"
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
"github.com/user-management-system/internal/service"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// WebhookHandler handles webhook requests
|
|
|
|
|
type WebhookHandler struct {
|
|
|
|
|
webhookService *service.WebhookService
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewWebhookHandler creates a new WebhookHandler
|
|
|
|
|
func NewWebhookHandler(webhookService *service.WebhookService) *WebhookHandler {
|
|
|
|
|
return &WebhookHandler{webhookService: webhookService}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 22:49:13 +08:00
|
|
|
// CreateWebhook 创建 Webhook
|
|
|
|
|
// @Summary 创建 Webhook
|
|
|
|
|
// @Description 创建新的 Webhook 配置
|
|
|
|
|
// @Tags Webhook管理
|
|
|
|
|
// @Accept json
|
|
|
|
|
// @Produce json
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
// @Param request body service.CreateWebhookRequest true "Webhook信息"
|
|
|
|
|
// @Success 201 {object} Response{data=domain.Webhook} "Webhook创建成功"
|
|
|
|
|
// @Failure 400 {object} Response "请求参数错误"
|
|
|
|
|
// @Failure 401 {object} Response "未认证"
|
|
|
|
|
// @Failure 500 {object} Response "服务器错误"
|
|
|
|
|
// @Router /api/v1/webhooks [post]
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
func (h *WebhookHandler) CreateWebhook(c *gin.Context) {
|
2026-04-08 20:06:54 +08:00
|
|
|
var req service.CreateWebhookRequest
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
userID, _ := c.Get("user_id")
|
|
|
|
|
creatorID, _ := userID.(int64)
|
|
|
|
|
|
|
|
|
|
webhook, err := h.webhookService.CreateWebhook(c.Request.Context(), &req, creatorID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": "创建 Webhook 失败"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 13:12:27 +08:00
|
|
|
c.JSON(http.StatusCreated, gin.H{"code": 0, "message": "success", "data": webhook})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-11 22:49:13 +08:00
|
|
|
// ListWebhooks 获取 Webhook 列表
|
|
|
|
|
// @Summary 获取 Webhook 列表
|
|
|
|
|
// @Description 获取当前用户的 Webhook 配置列表
|
|
|
|
|
// @Tags Webhook管理
|
|
|
|
|
// @Accept json
|
|
|
|
|
// @Produce json
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
// @Param page query int false "页码" default(1)
|
|
|
|
|
// @Param page_size query int false "每页数量" default(20)
|
|
|
|
|
// @Success 200 {object} Response "Webhook列表"
|
|
|
|
|
// @Failure 401 {object} Response "未认证"
|
|
|
|
|
// @Failure 500 {object} Response "服务器错误"
|
|
|
|
|
// @Router /api/v1/webhooks [get]
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
func (h *WebhookHandler) ListWebhooks(c *gin.Context) {
|
2026-04-08 20:06:54 +08:00
|
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
2026-05-08 12:40:36 +08:00
|
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", strconv.Itoa(pagination.DefaultPageSize)))
|
2026-04-08 20:06:54 +08:00
|
|
|
if page < 1 {
|
|
|
|
|
page = 1
|
|
|
|
|
}
|
2026-05-08 12:40:36 +08:00
|
|
|
if pageSize < 1 || pageSize > pagination.MaxPageSize {
|
|
|
|
|
pageSize = pagination.DefaultPageSize
|
2026-04-08 20:06:54 +08:00
|
|
|
}
|
|
|
|
|
offset := (page - 1) * pageSize
|
|
|
|
|
|
|
|
|
|
userID, _ := c.Get("user_id")
|
|
|
|
|
creatorID, _ := userID.(int64)
|
|
|
|
|
|
|
|
|
|
webhooks, total, err := h.webhookService.ListWebhooksPaginated(c.Request.Context(), creatorID, offset, pageSize)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": "获取 Webhook 列表失败"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
2026-04-11 13:12:27 +08:00
|
|
|
"code": 0,
|
|
|
|
|
"message": "success",
|
|
|
|
|
"data": gin.H{
|
|
|
|
|
"list": webhooks,
|
|
|
|
|
"total": total,
|
|
|
|
|
"page": page,
|
|
|
|
|
"page_size": pageSize,
|
|
|
|
|
},
|
2026-04-08 20:06:54 +08:00
|
|
|
})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-11 22:49:13 +08:00
|
|
|
// UpdateWebhook 更新 Webhook
|
|
|
|
|
// @Summary 更新 Webhook
|
|
|
|
|
// @Description 更新指定 Webhook 的配置
|
|
|
|
|
// @Tags Webhook管理
|
|
|
|
|
// @Accept json
|
|
|
|
|
// @Produce json
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
// @Param id path int true "Webhook ID"
|
|
|
|
|
// @Param request body service.UpdateWebhookRequest true "更新信息"
|
|
|
|
|
// @Success 200 {object} Response "更新成功"
|
|
|
|
|
// @Failure 400 {object} Response "请求参数错误"
|
|
|
|
|
// @Failure 401 {object} Response "未认证"
|
|
|
|
|
// @Failure 500 {object} Response "服务器错误"
|
|
|
|
|
// @Router /api/v1/webhooks/{id} [put]
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
func (h *WebhookHandler) UpdateWebhook(c *gin.Context) {
|
2026-04-08 20:06:54 +08:00
|
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "无效的 Webhook ID"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var req service.UpdateWebhookRequest
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := h.webhookService.UpdateWebhook(c.Request.Context(), id, &req); err != nil {
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": "更新 Webhook 失败"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "更新成功"})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-11 22:49:13 +08:00
|
|
|
// DeleteWebhook 删除 Webhook
|
|
|
|
|
// @Summary 删除 Webhook
|
|
|
|
|
// @Description 删除指定的 Webhook 配置
|
|
|
|
|
// @Tags Webhook管理
|
|
|
|
|
// @Produce json
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
// @Param id path int true "Webhook ID"
|
|
|
|
|
// @Success 200 {object} Response "删除成功"
|
|
|
|
|
// @Failure 400 {object} Response "请求参数错误"
|
|
|
|
|
// @Failure 401 {object} Response "未认证"
|
|
|
|
|
// @Failure 500 {object} Response "服务器错误"
|
|
|
|
|
// @Router /api/v1/webhooks/{id} [delete]
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
func (h *WebhookHandler) DeleteWebhook(c *gin.Context) {
|
2026-04-08 20:06:54 +08:00
|
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "无效的 Webhook ID"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := h.webhookService.DeleteWebhook(c.Request.Context(), id); err != nil {
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": "删除 Webhook 失败"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "删除成功"})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-11 22:49:13 +08:00
|
|
|
// GetWebhookDeliveries 获取 Webhook 投递记录
|
|
|
|
|
// @Summary 获取 Webhook 投递记录
|
|
|
|
|
// @Description 获取指定 Webhook 的最近投递记录
|
|
|
|
|
// @Tags Webhook管理
|
|
|
|
|
// @Produce json
|
|
|
|
|
// @Security BearerAuth
|
|
|
|
|
// @Param id path int true "Webhook ID"
|
|
|
|
|
// @Param limit query int false "返回记录数量" default(20)
|
|
|
|
|
// @Success 200 {object} Response "投递记录列表"
|
|
|
|
|
// @Failure 400 {object} Response "请求参数错误"
|
|
|
|
|
// @Failure 401 {object} Response "未认证"
|
|
|
|
|
// @Failure 500 {object} Response "服务器错误"
|
|
|
|
|
// @Router /api/v1/webhooks/{id}/deliveries [get]
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
func (h *WebhookHandler) GetWebhookDeliveries(c *gin.Context) {
|
2026-04-08 20:06:54 +08:00
|
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "无效的 Webhook ID"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-08 12:40:36 +08:00
|
|
|
limit, _ := strconv.Atoi(c.DefaultQuery("limit", strconv.Itoa(pagination.DefaultPageSize)))
|
|
|
|
|
if limit < 1 || limit > pagination.MaxPageSize {
|
|
|
|
|
limit = pagination.DefaultPageSize
|
2026-04-08 20:06:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deliveries, err := h.webhookService.GetWebhookDeliveries(c.Request.Context(), id, limit)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": "获取投递记录失败"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 13:12:27 +08:00
|
|
|
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success", "data": gin.H{"deliveries": deliveries}})
|
feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers
2026-04-02 11:19:50 +08:00
|
|
|
}
|