Files
ez-api/internal/api/log_webhook_handler.go
zenfun 33838b1e2c feat(api): wrap JSON responses in envelope
Add response envelope middleware to standardize JSON responses as
`{code,data,message}` with consistent business codes across endpoints.
Update Swagger annotations and tests to reflect the new response shape.

BREAKING CHANGE: API responses are now wrapped in a response envelope; clients must read payloads from `data` and handle `code`/`message` fields.
2026-01-10 00:15:08 +08:00

61 lines
2.0 KiB
Go

package api
import (
"net/http"
"github.com/ez-api/ez-api/internal/service"
"github.com/gin-gonic/gin"
)
// GetLogWebhookConfig godoc
// @Summary Get log webhook config
// @Description Returns current webhook notification config
// @Tags admin
// @Produce json
// @Security AdminAuth
// @Success 200 {object} ResponseEnvelope{data=service.LogWebhookConfig}
// @Failure 500 {object} ResponseEnvelope{data=gin.H}
// @Router /admin/logs/webhook [get]
func (h *Handler) GetLogWebhookConfig(c *gin.Context) {
if h == nil || h.logWebhook == nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "webhook service not configured"})
return
}
cfg, err := h.logWebhook.GetConfig(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to read webhook config", "details": err.Error()})
return
}
c.JSON(http.StatusOK, cfg)
}
// UpdateLogWebhookConfig godoc
// @Summary Update log webhook config
// @Description Updates webhook notification config
// @Tags admin
// @Accept json
// @Produce json
// @Security AdminAuth
// @Param request body service.LogWebhookConfig true "Webhook config"
// @Success 200 {object} ResponseEnvelope{data=service.LogWebhookConfig}
// @Failure 400 {object} ResponseEnvelope{data=gin.H}
// @Failure 500 {object} ResponseEnvelope{data=gin.H}
// @Router /admin/logs/webhook [put]
func (h *Handler) UpdateLogWebhookConfig(c *gin.Context) {
if h == nil || h.logWebhook == nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "webhook service not configured"})
return
}
var req service.LogWebhookConfig
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
cfg, err := h.logWebhook.SetConfig(c.Request.Context(), req)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, cfg)
}