2020-09-21 19:51:50 +08:00
|
|
|
package locations
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
2021-01-03 20:25:15 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/dao"
|
2020-09-21 19:51:50 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
2021-06-09 21:45:26 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
2020-09-21 19:51:50 +08:00
|
|
|
"github.com/iwind/TeaGo/actions"
|
|
|
|
|
"regexp"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2021-07-13 14:28:06 +08:00
|
|
|
// CreateAction 创建路由规则
|
2020-09-21 19:51:50 +08:00
|
|
|
type CreateAction struct {
|
|
|
|
|
actionutils.ParentAction
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *CreateAction) Init() {
|
|
|
|
|
this.Nav("", "setting", "create")
|
|
|
|
|
this.SecondMenu("locations")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *CreateAction) RunGet(params struct {
|
|
|
|
|
ServerId int64
|
2020-09-28 16:25:26 +08:00
|
|
|
ParentId int64 // 父节点
|
2020-09-21 19:51:50 +08:00
|
|
|
}) {
|
2021-01-03 20:25:15 +08:00
|
|
|
webConfig, err := dao.SharedHTTPWebDAO.FindWebConfigWithServerId(this.AdminContext(), params.ServerId)
|
2020-09-21 19:51:50 +08:00
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.Data["webId"] = webConfig.Id
|
|
|
|
|
this.Data["patternTypes"] = serverconfigs.AllLocationPatternTypes()
|
|
|
|
|
|
|
|
|
|
this.Show()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *CreateAction) RunPost(params struct {
|
|
|
|
|
WebId int64
|
|
|
|
|
|
|
|
|
|
Name string
|
|
|
|
|
Pattern string
|
|
|
|
|
PatternType int
|
|
|
|
|
Description string
|
|
|
|
|
|
|
|
|
|
IsBreak bool
|
|
|
|
|
IsCaseInsensitive bool
|
|
|
|
|
IsReverse bool
|
2021-06-09 21:45:26 +08:00
|
|
|
CondsJSON []byte
|
2020-09-21 19:51:50 +08:00
|
|
|
|
|
|
|
|
Must *actions.Must
|
|
|
|
|
}) {
|
|
|
|
|
params.Must.
|
|
|
|
|
Field("pattern", params.Pattern).
|
|
|
|
|
Require("请输入路径匹配规则")
|
|
|
|
|
|
|
|
|
|
// 校验正则
|
|
|
|
|
if params.PatternType == serverconfigs.HTTPLocationPatternTypeRegexp {
|
|
|
|
|
_, err := regexp.Compile(params.Pattern)
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.Fail("正则表达式校验失败:" + err.Error())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-09 21:45:26 +08:00
|
|
|
// 校验匹配条件
|
|
|
|
|
if len(params.CondsJSON) > 0 {
|
|
|
|
|
conds := &shared.HTTPRequestCondsConfig{}
|
|
|
|
|
err := json.Unmarshal(params.CondsJSON, conds)
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.Fail("匹配条件校验失败:" + err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = conds.Init()
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.Fail("匹配条件校验失败:" + err.Error())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-21 19:51:50 +08:00
|
|
|
// 自动加上前缀斜杠
|
|
|
|
|
if params.PatternType == serverconfigs.HTTPLocationPatternTypePrefix ||
|
|
|
|
|
params.PatternType == serverconfigs.HTTPLocationPatternTypeExact {
|
|
|
|
|
params.Pattern = "/" + strings.TrimLeft(params.Pattern, "/")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
location := &serverconfigs.HTTPLocationConfig{}
|
|
|
|
|
location.SetPattern(params.Pattern, params.PatternType, params.IsCaseInsensitive, params.IsReverse)
|
|
|
|
|
resultPattern := location.Pattern
|
|
|
|
|
|
|
|
|
|
locationResp, err := this.RPC().HTTPLocationRPC().CreateHTTPLocation(this.AdminContext(), &pb.CreateHTTPLocationRequest{
|
|
|
|
|
ParentId: 0, // TODO 需要实现
|
|
|
|
|
Name: params.Name,
|
|
|
|
|
Description: params.Description,
|
|
|
|
|
Pattern: resultPattern,
|
|
|
|
|
IsBreak: params.IsBreak,
|
2021-06-09 21:45:26 +08:00
|
|
|
CondsJSON: params.CondsJSON,
|
2020-09-21 19:51:50 +08:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
locationId := locationResp.LocationId
|
|
|
|
|
|
|
|
|
|
// Web中Location
|
2021-01-03 20:25:15 +08:00
|
|
|
webConfig, err := dao.SharedHTTPWebDAO.FindWebConfigWithId(this.AdminContext(), params.WebId)
|
2020-09-21 19:51:50 +08:00
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// TODO 支持Location嵌套
|
|
|
|
|
webConfig.LocationRefs = append(webConfig.LocationRefs, &serverconfigs.HTTPLocationRef{
|
|
|
|
|
IsOn: true,
|
|
|
|
|
LocationId: locationId,
|
|
|
|
|
Children: nil,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
refJSON, err := json.Marshal(webConfig.LocationRefs)
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
_, err = this.RPC().HTTPWebRPC().UpdateHTTPWebLocations(this.AdminContext(), &pb.UpdateHTTPWebLocationsRequest{
|
2021-11-24 11:58:01 +08:00
|
|
|
HttpWebId: params.WebId,
|
2020-09-21 19:51:50 +08:00
|
|
|
LocationsJSON: refJSON,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.Success()
|
|
|
|
|
}
|