This commit is contained in:
2026-06-19 10:56:52 +08:00
parent d71a23fa57
commit 2dcba2e5d9
10 changed files with 310 additions and 80 deletions

View File

@@ -13,7 +13,7 @@ import (
"go.uber.org/zap"
)
// PublicQueryWhitelistHandler 查询白名单公开添加接口(面向下游调用方)
// PublicQueryWhitelistHandler 查询白名单公开接口(面向下游调用方)
type PublicQueryWhitelistHandler struct {
appService api_app.QueryWhitelistApplicationService
validator interfaces.RequestValidator
@@ -34,6 +34,21 @@ func NewPublicQueryWhitelistHandler(
// CreateEntry 公开添加查询白名单规则
func (h *PublicQueryWhitelistHandler) CreateEntry(c *gin.Context) {
h.handlePublicEntry(c, func(ctx *gin.Context, accessID, mgmtKey, clientIP, data string) (any, string, error) {
return h.appService.CreateEntryPublic(ctx.Request.Context(), accessID, mgmtKey, clientIP, data)
})
}
// AppendEntry 向已有规则追加生效接口(去重合并)
func (h *PublicQueryWhitelistHandler) AppendEntry(c *gin.Context) {
h.handlePublicEntry(c, func(ctx *gin.Context, accessID, mgmtKey, clientIP, data string) (any, string, error) {
return h.appService.AppendEntryPublic(ctx.Request.Context(), accessID, mgmtKey, clientIP, data)
})
}
type publicEntryHandler func(*gin.Context, string, string, string, string) (any, string, error)
func (h *PublicQueryWhitelistHandler) handlePublicEntry(c *gin.Context, fn publicEntryHandler) {
accessID := c.GetHeader("Access-Id")
if accessID == "" {
c.JSON(200, dto.NewErrorResponse(1005, "缺少Access-Id", ""))
@@ -53,13 +68,7 @@ func (h *PublicQueryWhitelistHandler) CreateEntry(c *gin.Context) {
}
transactionID := uuid.New().String()
result, secretKey, err := h.appService.CreateEntryPublic(
c.Request.Context(),
accessID,
mgmtKey,
c.ClientIP(),
req.Data,
)
result, secretKey, err := fn(c, accessID, mgmtKey, c.ClientIP(), req.Data)
if err != nil {
code := api_app.GetErrorCode(err)
message := api_app.PublicAPIErrorMessage(err)

View File

@@ -31,5 +31,6 @@ func (r *PublicQueryWhitelistRoutes) Register(router *sharedhttp.GinRouter) {
group := router.GetEngine().Group("/api/v1/query-whitelist")
group.Use(r.domainAuthMiddleware.Handle(""))
group.POST("/entries", r.handler.CreateEntry)
group.POST("/entries/append", r.handler.AppendEntry)
r.logger.Info("查询白名单公开接口路由注册完成")
}