实现特殊页面配置

This commit is contained in:
GoEdgeLab
2020-09-20 08:58:48 +08:00
parent b20ab500aa
commit 6fddbc6a33
13 changed files with 400 additions and 3 deletions

View File

@@ -31,6 +31,7 @@ type RPCClient struct {
httpGzipClients []pb.HTTPGzipServiceClient
httpHeaderPolicyClients []pb.HTTPHeaderPolicyServiceClient
httpHeaderClients []pb.HTTPHeaderServiceClient
httpPageClients []pb.HTTPPageServiceClient
}
func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
@@ -51,6 +52,7 @@ func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
httpGzipClients := []pb.HTTPGzipServiceClient{}
httpHeaderPolicyClients := []pb.HTTPHeaderPolicyServiceClient{}
httpHeaderClients := []pb.HTTPHeaderServiceClient{}
httpPageClients := []pb.HTTPPageServiceClient{}
conns := []*grpc.ClientConn{}
for _, endpoint := range apiConfig.RPC.Endpoints {
@@ -79,6 +81,7 @@ func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
httpGzipClients = append(httpGzipClients, pb.NewHTTPGzipServiceClient(conn))
httpHeaderPolicyClients = append(httpHeaderPolicyClients, pb.NewHTTPHeaderPolicyServiceClient(conn))
httpHeaderClients = append(httpHeaderClients, pb.NewHTTPHeaderServiceClient(conn))
httpPageClients = append(httpPageClients, pb.NewHTTPPageServiceClient(conn))
}
return &RPCClient{
@@ -96,6 +99,7 @@ func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
httpGzipClients: httpGzipClients,
httpHeaderPolicyClients: httpHeaderPolicyClients,
httpHeaderClients: httpHeaderClients,
httpPageClients: httpPageClients,
}, nil
}
@@ -190,6 +194,13 @@ func (this *RPCClient) HTTPHeaderPolicyRPC() pb.HTTPHeaderPolicyServiceClient {
return nil
}
func (this *RPCClient) HTTPPageRPC() pb.HTTPPageServiceClient {
if len(this.httpPageClients) > 0 {
return this.httpPageClients[rands.Int(0, len(this.httpPageClients)-1)]
}
return nil
}
func (this *RPCClient) Context(adminId int64) context.Context {
ctx := context.Background()
m := maps.Map{

View File

@@ -0,0 +1,64 @@
package pages
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/types"
)
type CreatePopupAction struct {
actionutils.ParentAction
}
func (this *CreatePopupAction) Init() {
this.Nav("", "", "")
}
func (this *CreatePopupAction) RunGet(params struct{}) {
this.Show()
}
func (this *CreatePopupAction) RunPost(params struct {
Status string
URL string `alias:"url"`
NewStatus int
Must *actions.Must
}) {
// TODO 对状态码进行更多校验
params.Must.
Field("status", params.Status).
Require("请输入响应状态码").
Field("url", params.URL).
Require("请输入要显示的URL")
createResp, err := this.RPC().HTTPPageRPC().CreateHTTPPage(this.AdminContext(), &pb.CreateHTTPPageRequest{
StatusList: []string{params.Status},
Url: params.URL,
NewStatus: types.Int32(params.NewStatus),
})
if err != nil {
this.ErrorPage(err)
return
}
pageId := createResp.PageId
configResp, err := this.RPC().HTTPPageRPC().FindEnabledHTTPPageConfig(this.AdminContext(), &pb.FindEnabledHTTPPageConfigRequest{PageId: pageId})
if err != nil {
this.ErrorPage(err)
return
}
pageConfig := &serverconfigs.HTTPPageConfig{}
err = json.Unmarshal(configResp.Config, pageConfig)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["page"] = pageConfig
this.Success()
}

View File

@@ -1,7 +1,11 @@
package pages
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/actions"
)
type IndexAction struct {
@@ -16,7 +20,50 @@ func (this *IndexAction) Init() {
func (this *IndexAction) RunGet(params struct {
ServerId int64
}) {
// TODO
webResp, err := this.RPC().ServerRPC().FindAndInitServerWebConfig(this.AdminContext(), &pb.FindAndInitServerWebRequest{ServerId: params.ServerId})
if err != nil {
this.ErrorPage(err)
return
}
webConfig := &serverconfigs.HTTPWebConfig{}
err = json.Unmarshal(webResp.Config, webConfig)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["webId"] = webConfig.Id
this.Data["pages"] = webConfig.Pages
this.Data["shutdownConfig"] = webConfig.Shutdown
this.Show()
}
func (this *IndexAction) RunPost(params struct {
WebId int64
PagesJSON string
ShutdownJSON string
Must *actions.Must
}) {
// TODO 检查配置
_, err := this.RPC().HTTPWebRPC().UpdateHTTPWebPages(this.AdminContext(), &pb.UpdateHTTPWebPagesRequest{
WebId: params.WebId,
PagesJSON: []byte(params.PagesJSON),
})
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().HTTPWebRPC().UpdateHTTPWebShutdown(this.AdminContext(), &pb.UpdateHTTPWebShutdownRequest{
WebId: params.WebId,
ShutdownJSON: []byte(params.ShutdownJSON),
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -12,7 +12,9 @@ func init() {
Helper(helpers.NewUserMustAuth()).
Helper(serverutils.NewServerHelper()).
Prefix("/servers/server/settings/pages").
Get("", new(IndexAction)).
GetPost("", new(IndexAction)).
GetPost("/createPopup", new(CreatePopupAction)).
GetPost("/updatePopup", new(UpdatePopupAction)).
EndAll()
})
}

View File

@@ -0,0 +1,82 @@
package pages
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/types"
)
type UpdatePopupAction struct {
actionutils.ParentAction
}
func (this *UpdatePopupAction) Init() {
this.Nav("", "", "")
}
func (this *UpdatePopupAction) RunGet(params struct {
PageId int64
}) {
configResp, err := this.RPC().HTTPPageRPC().FindEnabledHTTPPageConfig(this.AdminContext(), &pb.FindEnabledHTTPPageConfigRequest{PageId: params.PageId})
if err != nil {
this.ErrorPage(err)
return
}
pageConfig := &serverconfigs.HTTPPageConfig{}
err = json.Unmarshal(configResp.Config, pageConfig)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["pageConfig"] = pageConfig
this.Show()
}
func (this *UpdatePopupAction) RunPost(params struct {
PageId int64
Status string
URL string `alias:"url"`
NewStatus int
Must *actions.Must
}) {
params.Must.
Field("status", params.Status).
Require("请输入响应状态码").
Field("url", params.URL).
Require("请输入要显示的URL")
_, err := this.RPC().HTTPPageRPC().UpdateHTTPPage(this.AdminContext(), &pb.UpdateHTTPPageRequest{
PageId: params.PageId,
StatusList: []string{params.Status},
Url: params.URL,
NewStatus: types.Int32(params.NewStatus),
})
if err != nil {
this.ErrorPage(err)
return
}
// 返回修改后的配置
configResp, err := this.RPC().HTTPPageRPC().FindEnabledHTTPPageConfig(this.AdminContext(), &pb.FindEnabledHTTPPageConfigRequest{PageId: params.PageId})
if err != nil {
this.ErrorPage(err)
return
}
pageConfig := &serverconfigs.HTTPPageConfig{}
err = json.Unmarshal(configResp.Config, pageConfig)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["page"] = pageConfig
this.Success()
}

View File

@@ -40,6 +40,9 @@ func (this *ServerHelper) createLeftMenu(action *actions.ActionObject) {
secondMenuItem, _ := action.Data["secondMenuItem"]
serverId := action.ParamInt64("serverId")
if serverId == 0 {
return
}
serverIdString := strconv.FormatInt(serverId, 10)
action.Data["serverId"] = serverId

View File

@@ -0,0 +1,113 @@
Vue.component("pages-and-shutdown-box", {
props: ["v-pages", "v-shutdown-config"],
data: function () {
let pages = []
if (this.vPages != null) {
pages = this.vPages
}
let shutdownConfig = {
isOn: false,
url: "",
status: 0
}
if (this.vShutdownConfig != null) {
shutdownConfig = this.vShutdownConfig
}
let shutdownStatus = ""
if (shutdownConfig.status > 0) {
shutdownStatus = shutdownConfig.status.toString()
}
return {
pages: pages,
shutdownConfig: shutdownConfig,
shutdownStatus: shutdownStatus
}
},
watch: {
shutdownStatus: function (status) {
let statusInt = parseInt(status)
if (!isNaN(statusInt) && statusInt > 0 && statusInt < 1000) {
this.shutdownConfig.status = statusInt
} else {
this.shutdownConfig.status = 0
}
}
},
methods: {
addPage: function () {
let that = this
teaweb.popup("/servers/server/settings/pages/createPopup", {
height: "22em",
callback: function (resp) {
that.pages.push(resp.data.page)
}
})
},
updatePage: function (pageIndex, pageId) {
let that = this
teaweb.popup("/servers/server/settings/pages/updatePopup?pageId=" + pageId, {
height: "22em",
callback: function (resp) {
Vue.set(that.pages, pageIndex, resp.data.page)
}
})
},
removePage: function (pageIndex) {
let that = this
teaweb.confirm("确定要移除此页面吗?", function () {
that.pages.$remove(pageIndex)
})
}
},
template: `<div>
<input type="hidden" name="pagesJSON" :value="JSON.stringify(pages)"/>
<input type="hidden" name="shutdownJSON" :value="JSON.stringify(shutdownConfig)"/>
<table class="ui table selectable definition">
<tr>
<td class="title">特殊页面</td>
<td>
<div v-if="pages.length > 0">
<div class="ui label small" v-for="(page,index) in pages">
{{page.status}} -&gt; {{page.url}} <a href="" title="修改" @click.prevent="updatePage(index, page.id)"><i class="icon pencil small"></i></a> <a href="" title="删除" @click.prevent="removePage(index)"><i class="icon remove"></i></a>
</div>
<div class="ui divider"></div>
</div>
<div>
<button class="ui button small" type="button" @click.prevent="addPage()">+</button>
</div>
<p class="comment">根据响应状态码返回一些特殊页面比如404500等错误页面。</p>
</td>
</tr>
<tr>
<td>临时关闭页面</td>
<td>
<div>
<div class="ui checkbox">
<input type="checkbox" value="1" v-model="shutdownConfig.isOn" />
<label></label>
</div>
<div v-show="shutdownConfig.isOn">
<table class="ui table selectable definition">
<tr>
<td class="title">页面URL</td>
<td>
<input type="text" v-model="shutdownConfig.url" placeholder="页面文件路径或一个完整URL"/>
<p class="comment">页面文件是相对于节点安装目录的页面文件比如web/pages/40x.html或者一个完整的URL。</p>
</td>
</tr>
<tr>
<td>状态码</td>
<td><input type="text" size="3" maxlength="3" name="shutdownStatus" style="width:5.2em" placeholder="状态码" v-model="shutdownStatus"/></td>
</tr>
</table>
</div>
<p class="comment">开启临时关闭页面时,所有请求的响应都会显示此页面。可用于临时升级网站使用。</p>
</div>
</td>
</tr>
</table>
<div class="ui margin"></div>
</div>`
})

View File

@@ -0,0 +1,28 @@
{$layout "layout_popup"}
<h3>添加特殊页面</h3>
<form class="ui form" data-tea-action="$" data-tea-success="success">
<table class="ui table selectable definition">
<tr>
<td class="title">响应状态码 *</td>
<td>
<input type="text" name="status" size="3" placeholder="状态码" maxlength="3" style="width:5.2em" ref="focus"/>
<p class="comment">比如404或者50x。</p>
</td>
</tr>
<tr>
<td>URL *</td>
<td>
<input type="text" name="url" maxlength="500" placeholder="页面文件路径或者完整的URL"/>
<p class="comment">页面文件是相对于节点安装目录的页面文件比如web/pages/40x.html或者一个完整的URL。</p>
</td>
</tr>
<tr>
<td>新状态码</td>
<td>
<input type="text" name="newStatus" size="3" placeholder="状态码" maxlength="3" style="width:5.2em"/>
<p class="comment">可以用来修改响应的状态码,不填表示不改变原有状态码。</p>
</td>
</tr>
</table>
<submit-btn></submit-btn>
</form>

View File

@@ -0,0 +1,3 @@
Tea.context(function () {
this.success = NotifyPopup
})

View File

@@ -3,5 +3,9 @@
{$template "/left_menu"}
<div class="right-box">
<p class="ui message">此功能暂未开放,敬请期待。</p>
<form class="ui form" data-tea-action="$" data-tea-success="success">
<input type="hidden" name="webId" :value="webId"/>
<pages-and-shutdown-box :v-pages="pages" :v-shutdown-config="shutdownConfig"></pages-and-shutdown-box>
<submit-btn></submit-btn>
</form>
</div>

View File

@@ -0,0 +1,3 @@
Tea.context(function () {
this.success = NotifyReloadSuccess("保存成功")
})

View File

@@ -0,0 +1,29 @@
{$layout "layout_popup"}
<h3>修改特殊页面</h3>
<form class="ui form" data-tea-action="$" data-tea-success="success">
<input type="hidden" name="pageId" :value="pageConfig.id"/>
<table class="ui table selectable definition">
<tr>
<td class="title">响应状态码 *</td>
<td>
<input type="text" name="status" size="3" placeholder="状态码" maxlength="3" style="width:5.2em" ref="focus" v-model="pageConfig.status"/>
<p class="comment">比如404或者50x。</p>
</td>
</tr>
<tr>
<td>URL *</td>
<td>
<input type="text" name="url" maxlength="500" placeholder="页面文件路径或者完整的URL" v-model="pageConfig.url"/>
<p class="comment">页面文件是相对于节点安装目录的页面文件比如web/pages/40x.html或者一个完整的URL。</p>
</td>
</tr>
<tr>
<td>新状态码</td>
<td>
<input type="text" name="newStatus" size="3" placeholder="状态码" maxlength="3" style="width:5.2em" v-model="newStatus"/>
<p class="comment">可以用来修改响应的状态码,不填表示不改变原有状态码。</p>
</td>
</tr>
</table>
<submit-btn></submit-btn>
</form>

View File

@@ -0,0 +1,8 @@
Tea.context(function () {
this.success = NotifyPopup
this.newStatus = ""
if (this.pageConfig.newStatus > 0) {
this.newStatus = this.pageConfig.newStatus
}
})