f
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
api_app "tyapi-server/internal/application/api"
|
||||
"tyapi-server/internal/application/api/dto"
|
||||
"tyapi-server/internal/shared/crypto"
|
||||
"tyapi-server/internal/shared/interfaces"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// PublicQueryWhitelistHandler 查询白名单公开添加接口(面向下游调用方)
|
||||
type PublicQueryWhitelistHandler struct {
|
||||
appService api_app.QueryWhitelistApplicationService
|
||||
validator interfaces.RequestValidator
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
func NewPublicQueryWhitelistHandler(
|
||||
appService api_app.QueryWhitelistApplicationService,
|
||||
validator interfaces.RequestValidator,
|
||||
logger *zap.Logger,
|
||||
) *PublicQueryWhitelistHandler {
|
||||
return &PublicQueryWhitelistHandler{
|
||||
appService: appService,
|
||||
validator: validator,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// CreateEntry 公开添加查询白名单规则
|
||||
func (h *PublicQueryWhitelistHandler) CreateEntry(c *gin.Context) {
|
||||
accessID := c.GetHeader("Access-Id")
|
||||
if accessID == "" {
|
||||
c.JSON(200, dto.NewErrorResponse(1005, "缺少Access-Id", ""))
|
||||
return
|
||||
}
|
||||
|
||||
mgmtKey := c.GetHeader(api_app.QueryWhitelistMgmtKeyHeader())
|
||||
if mgmtKey == "" {
|
||||
c.JSON(200, dto.NewErrorResponse(api_app.GetErrorCode(api_app.ErrMissingMgmtKey), "缺少管理密钥", ""))
|
||||
return
|
||||
}
|
||||
|
||||
var req dto.QueryWhitelistPublicEncryptedRequest
|
||||
if err := h.validator.BindAndValidate(c, &req); err != nil {
|
||||
c.JSON(200, dto.NewErrorResponse(1003, "请求参数结构不正确", ""))
|
||||
return
|
||||
}
|
||||
|
||||
transactionID := uuid.New().String()
|
||||
result, secretKey, err := h.appService.CreateEntryPublic(
|
||||
c.Request.Context(),
|
||||
accessID,
|
||||
mgmtKey,
|
||||
c.ClientIP(),
|
||||
req.Data,
|
||||
)
|
||||
if err != nil {
|
||||
code := api_app.GetErrorCode(err)
|
||||
message := api_app.PublicAPIErrorMessage(err)
|
||||
c.JSON(200, dto.NewErrorResponse(code, message, transactionID))
|
||||
return
|
||||
}
|
||||
|
||||
respBytes, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
h.logger.Error("序列化公开白名单响应失败", zap.Error(err))
|
||||
c.JSON(200, dto.NewErrorResponse(1001, "接口异常", transactionID))
|
||||
return
|
||||
}
|
||||
|
||||
encrypted, err := crypto.AesEncrypt(respBytes, secretKey)
|
||||
if err != nil {
|
||||
h.logger.Error("加密公开白名单响应失败", zap.Error(err))
|
||||
c.JSON(200, dto.NewErrorResponse(1001, "接口异常", transactionID))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, dto.NewSuccessResponse(transactionID, encrypted))
|
||||
}
|
||||
Reference in New Issue
Block a user