mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-27 01:50:31 +08:00
HTTP Header:实现请求方法、域名、状态码等限制,实现内容替换功能
This commit is contained in:
@@ -4,14 +4,16 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||
)
|
||||
|
||||
type HTTPHeaderService struct {
|
||||
BaseService
|
||||
}
|
||||
|
||||
// 创建Header
|
||||
// CreateHTTPHeader 创建Header
|
||||
func (this *HTTPHeaderService) CreateHTTPHeader(ctx context.Context, req *pb.CreateHTTPHeaderRequest) (*pb.CreateHTTPHeaderResponse, error) {
|
||||
// 校验请求
|
||||
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||
@@ -25,7 +27,22 @@ func (this *HTTPHeaderService) CreateHTTPHeader(ctx context.Context, req *pb.Cre
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
headerId, err := models.SharedHTTPHeaderDAO.CreateHeader(tx, req.Name, req.Value)
|
||||
// status
|
||||
var newStatus = []int{}
|
||||
for _, status := range req.Status {
|
||||
newStatus = append(newStatus, int(status))
|
||||
}
|
||||
|
||||
// replace values
|
||||
var replaceValues = []*shared.HTTPHeaderReplaceValue{}
|
||||
if len(req.ReplaceValuesJSON) > 0 {
|
||||
err = json.Unmarshal(req.ReplaceValuesJSON, &replaceValues)
|
||||
if err != nil {
|
||||
return nil, errors.New("decode replace values failed: " + err.Error() + ", json: " + string(req.ReplaceValuesJSON))
|
||||
}
|
||||
}
|
||||
|
||||
headerId, err := models.SharedHTTPHeaderDAO.CreateHeader(tx, userId, req.Name, req.Value, newStatus, req.DisableRedirect, req.ShouldAppend, req.ShouldReplace, replaceValues, req.Methods, req.Domains)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -33,7 +50,7 @@ func (this *HTTPHeaderService) CreateHTTPHeader(ctx context.Context, req *pb.Cre
|
||||
return &pb.CreateHTTPHeaderResponse{HeaderId: headerId}, nil
|
||||
}
|
||||
|
||||
// 修改Header
|
||||
// UpdateHTTPHeader 修改Header
|
||||
func (this *HTTPHeaderService) UpdateHTTPHeader(ctx context.Context, req *pb.UpdateHTTPHeaderRequest) (*pb.RPCSuccess, error) {
|
||||
// 校验请求
|
||||
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||
@@ -47,7 +64,22 @@ func (this *HTTPHeaderService) UpdateHTTPHeader(ctx context.Context, req *pb.Upd
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
err = models.SharedHTTPHeaderDAO.UpdateHeader(tx, req.HeaderId, req.Name, req.Value)
|
||||
// status
|
||||
var newStatus = []int{}
|
||||
for _, status := range req.Status {
|
||||
newStatus = append(newStatus, int(status))
|
||||
}
|
||||
|
||||
// replace values
|
||||
var replaceValues = []*shared.HTTPHeaderReplaceValue{}
|
||||
if len(req.ReplaceValuesJSON) > 0 {
|
||||
err = json.Unmarshal(req.ReplaceValuesJSON, &replaceValues)
|
||||
if err != nil {
|
||||
return nil, errors.New("decode replace values failed: " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
err = models.SharedHTTPHeaderDAO.UpdateHeader(tx, req.HeaderId, req.Name, req.Value, newStatus, req.DisableRedirect, req.ShouldAppend, req.ShouldReplace, replaceValues, req.Methods, req.Domains)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -55,7 +87,7 @@ func (this *HTTPHeaderService) UpdateHTTPHeader(ctx context.Context, req *pb.Upd
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// 查找配置
|
||||
// FindEnabledHTTPHeaderConfig 查找配置
|
||||
func (this *HTTPHeaderService) FindEnabledHTTPHeaderConfig(ctx context.Context, req *pb.FindEnabledHTTPHeaderConfigRequest) (*pb.FindEnabledHTTPHeaderConfigResponse, error) {
|
||||
// 校验请求
|
||||
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||
|
||||
Reference in New Issue
Block a user