实现URL跳转功能

This commit is contained in:
刘祥超
2021-01-10 17:34:30 +08:00
parent 0940fce743
commit e70ba457b2
12 changed files with 293 additions and 9 deletions

View File

@@ -0,0 +1,79 @@
package redirects
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
"net/url"
)
type CreatePopupAction struct {
actionutils.ParentAction
}
func (this *CreatePopupAction) Init() {
this.Nav("", "", "")
}
func (this *CreatePopupAction) RunGet(params struct {
}) {
this.Data["statusList"] = serverconfigs.AllHTTPRedirectStatusList()
this.Show()
}
func (this *CreatePopupAction) RunPost(params struct {
BeforeURL string
AfterURL string
Status int
Must *actions.Must
CSRF *actionutils.CSRF
}) {
params.Must.
Field("beforeURL", params.BeforeURL).
Require("请填写跳转前的URL")
// 校验格式
{
u, err := url.Parse(params.BeforeURL)
if err != nil {
this.FailField("beforeURL", "请输入正确的跳转前URL")
}
if (u.Scheme != "http" && u.Scheme != "https") ||
len(u.Host) == 0 {
this.FailField("beforeURL", "请输入正确的跳转前URL")
}
}
params.Must.
Field("afterURL", params.AfterURL).
Require("请填写跳转后URL")
// 校验格式
{
u, err := url.Parse(params.AfterURL)
if err != nil {
this.FailField("afterURL", "请输入正确的跳转后URL")
}
if (u.Scheme != "http" && u.Scheme != "https") ||
len(u.Host) == 0 {
this.FailField("afterURL", "请输入正确的跳转后URL")
}
}
params.Must.
Field("status", params.Status).
Gte(0, "请选择正确的跳转状态码")
this.Data["redirect"] = maps.Map{
"status": params.Status,
"beforeURL": params.BeforeURL,
"afterURL": params.AfterURL,
"isOn": true,
}
this.Success()
}

View File

@@ -0,0 +1,54 @@
package redirects
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/dao"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "setting", "index")
this.SecondMenu("redirects")
}
func (this *IndexAction) RunGet(params struct {
ServerId int64
}) {
webConfig, err := dao.SharedHTTPWebDAO.FindWebConfigWithServerId(this.AdminContext(), params.ServerId)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["webId"] = webConfig.Id
this.Data["redirects"] = webConfig.HostRedirects
this.Show()
}
func (this *IndexAction) RunPost(params struct {
ServerId int64
WebId int64
HostRedirectsJSON []byte
Must *actions.Must
}) {
defer this.CreateLogInfo("修改Web %d 的URL跳转设置", params.WebId)
_, err := this.RPC().HTTPWebRPC().UpdateHTTPWebHostRedirects(this.AdminContext(), &pb.UpdateHTTPWebHostRedirectsRequest{
WebId: params.WebId,
HostRedirectsJSON: params.HostRedirectsJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -0,0 +1,20 @@
package redirects
import (
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/serverutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo"
)
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Helper(helpers.NewUserMustAuth(configloaders.AdminModuleCodeServer)).
Helper(serverutils.NewServerHelper()).
Prefix("/servers/server/settings/redirects").
GetPost("", new(IndexAction)).
GetPost("/createPopup", new(CreatePopupAction)).
EndAll()
})
}

View File

@@ -204,6 +204,12 @@ func (this *ServerHelper) createSettingsMenu(secondMenuItem string, serverIdStri
"url": "",
"isActive": false,
})
menuItems = append(menuItems, maps.Map{
"name": "URL跳转",
"url": "/servers/server/settings/redirects?serverId=" + serverIdString,
"isActive": secondMenuItem == "redirects",
"isOn": serverConfig.Web != nil && len(serverConfig.Web.HostRedirects) > 0,
})
menuItems = append(menuItems, maps.Map{
"name": "路径规则",
"url": "/servers/server/settings/locations?serverId=" + serverIdString,