mirror of
https://github.com/TeaOSLab/EdgeAdmin.git
synced 2025-11-03 04:10:27 +08:00
实现数据库节点管理
This commit is contained in:
@@ -87,6 +87,10 @@ func (this *RPCClient) APINodeRPC() pb.APINodeServiceClient {
|
||||
return pb.NewAPINodeServiceClient(this.pickConn())
|
||||
}
|
||||
|
||||
func (this *RPCClient) DBNodeRPC() pb.DBNodeServiceClient {
|
||||
return pb.NewDBNodeServiceClient(this.pickConn())
|
||||
}
|
||||
|
||||
func (this *RPCClient) OriginRPC() pb.OriginServiceClient {
|
||||
return pb.NewOriginServiceClient(this.pickConn())
|
||||
}
|
||||
|
||||
64
internal/web/actions/default/db/createPopup.go
Normal file
64
internal/web/actions/default/db/createPopup.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
)
|
||||
|
||||
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 {
|
||||
Name string
|
||||
Host string
|
||||
Port int32
|
||||
Database string
|
||||
Username string
|
||||
Password string
|
||||
|
||||
Description string
|
||||
IsOn bool
|
||||
|
||||
Must *actions.Must
|
||||
}) {
|
||||
params.Must.
|
||||
Field("name", params.Name).
|
||||
Require("请输入节点名称").
|
||||
Field("host", params.Host).
|
||||
Require("请输入主机地址").
|
||||
Field("port", params.Port).
|
||||
Gt(0, "请输入正确的数据库端口").
|
||||
Lt(65535, "请输入正确的数据库端口").
|
||||
Field("database", params.Database).
|
||||
Require("请输入数据库名称").
|
||||
Field("username", params.Username).
|
||||
Require("请输入连接数据库的用户名")
|
||||
|
||||
_, err := this.RPC().DBNodeRPC().CreateDBNode(this.AdminContext(), &pb.CreateDBNodeRequest{
|
||||
IsOn: params.IsOn,
|
||||
Name: params.Name,
|
||||
Description: params.Description,
|
||||
Host: params.Host,
|
||||
Port: params.Port,
|
||||
Database: params.Database,
|
||||
Username: params.Username,
|
||||
Password: params.Password,
|
||||
Charset: "", // 暂时不能修改
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Success()
|
||||
}
|
||||
22
internal/web/actions/default/db/delete.go
Normal file
22
internal/web/actions/default/db/delete.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
)
|
||||
|
||||
type DeleteAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *DeleteAction) RunPost(params struct {
|
||||
NodeId int64
|
||||
}) {
|
||||
_, err := this.RPC().DBNodeRPC().DeleteDBNode(this.AdminContext(), &pb.DeleteDBNodeRequest{NodeId: params.NodeId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Success()
|
||||
}
|
||||
@@ -1,6 +1,10 @@
|
||||
package db
|
||||
|
||||
import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
type IndexAction struct {
|
||||
actionutils.ParentAction
|
||||
@@ -11,5 +15,36 @@ func (this *IndexAction) Init() {
|
||||
}
|
||||
|
||||
func (this *IndexAction) RunGet(params struct{}) {
|
||||
countResp, err := this.RPC().DBNodeRPC().CountAllEnabledDBNodes(this.AdminContext(), &pb.CountAllEnabledDBNodesRequest{})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
count := countResp.Count
|
||||
|
||||
page := this.NewPage(count)
|
||||
listResp, err := this.RPC().DBNodeRPC().ListEnabledDBNodes(this.AdminContext(), &pb.ListEnabledDBNodesRequest{
|
||||
Offset: page.Offset,
|
||||
Size: page.Size,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
nodeMaps := []maps.Map{}
|
||||
for _, node := range listResp.Nodes {
|
||||
nodeMaps = append(nodeMaps, maps.Map{
|
||||
"id": node.Id,
|
||||
"isOn": node.IsOn,
|
||||
"name": node.Name,
|
||||
"host": node.Host,
|
||||
"port": node.Port,
|
||||
"database": node.Database,
|
||||
})
|
||||
}
|
||||
|
||||
this.Data["nodes"] = nodeMaps
|
||||
this.Data["page"] = page.AsHTML()
|
||||
|
||||
this.Show()
|
||||
}
|
||||
|
||||
@@ -12,6 +12,9 @@ func init() {
|
||||
Helper(new(Helper)).
|
||||
Prefix("/db").
|
||||
Get("", new(IndexAction)).
|
||||
GetPost("/createPopup", new(CreatePopupAction)).
|
||||
GetPost("/updatePopup", new(UpdatePopupAction)).
|
||||
Post("/delete", new(DeleteAction)).
|
||||
|
||||
EndAll()
|
||||
})
|
||||
|
||||
94
internal/web/actions/default/db/updatePopup.go
Normal file
94
internal/web/actions/default/db/updatePopup.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
type UpdatePopupAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *UpdatePopupAction) Init() {
|
||||
this.Nav("", "", "")
|
||||
}
|
||||
|
||||
func (this *UpdatePopupAction) RunGet(params struct {
|
||||
NodeId int64
|
||||
}) {
|
||||
nodeResp, err := this.RPC().DBNodeRPC().FindEnabledDBNode(this.AdminContext(), &pb.FindEnabledDBNodeRequest{NodeId: params.NodeId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
node := nodeResp.Node
|
||||
if node == nil {
|
||||
this.NotFound("dbNode", params.NodeId)
|
||||
return
|
||||
}
|
||||
|
||||
this.Data["node"] = maps.Map{
|
||||
"id": node.Id,
|
||||
"isOn": node.IsOn,
|
||||
"name": node.Name,
|
||||
"description": node.Description,
|
||||
"host": node.Host,
|
||||
"port": node.Port,
|
||||
"username": node.Username,
|
||||
"password": node.Password,
|
||||
"database": node.Database,
|
||||
}
|
||||
|
||||
this.Show()
|
||||
}
|
||||
|
||||
func (this *UpdatePopupAction) RunPost(params struct {
|
||||
NodeId int64
|
||||
|
||||
Name string
|
||||
Host string
|
||||
Port int32
|
||||
Database string
|
||||
Username string
|
||||
Password string
|
||||
|
||||
Description string
|
||||
IsOn bool
|
||||
|
||||
Must *actions.Must
|
||||
}) {
|
||||
params.Must.
|
||||
Field("name", params.Name).
|
||||
Require("请输入节点名称").
|
||||
Field("host", params.Host).
|
||||
Require("请输入主机地址").
|
||||
Field("port", params.Port).
|
||||
Gt(0, "请输入正确的数据库端口").
|
||||
Lt(65535, "请输入正确的数据库端口").
|
||||
Field("database", params.Database).
|
||||
Require("请输入数据库名称").
|
||||
Field("username", params.Username).
|
||||
Require("请输入连接数据库的用户名")
|
||||
|
||||
_, err := this.RPC().DBNodeRPC().UpdateDBNode(this.AdminContext(), &pb.UpdateDBNodeRequest{
|
||||
NodeId: params.NodeId,
|
||||
IsOn: params.IsOn,
|
||||
Name: params.Name,
|
||||
Description: params.Description,
|
||||
Host: params.Host,
|
||||
Port: params.Port,
|
||||
Database: params.Database,
|
||||
Username: params.Username,
|
||||
Password: params.Password,
|
||||
Charset: "", // 暂时不能修改
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Success()
|
||||
}
|
||||
@@ -82,7 +82,7 @@ func (this *UserMustAuth) BeforeAction(actionPtr actions.ActionWrapper, paramNam
|
||||
},
|
||||
{
|
||||
"code": "db",
|
||||
"menuName": "数据库",
|
||||
"menuName": "日志数据库",
|
||||
"icon": "database",
|
||||
},
|
||||
{
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
Vue.component("label-on", {
|
||||
props: ["v-is-on"],
|
||||
template: '<div><span v-if="vIsOn" class="ui label tiny green basic">已启用</span><span v-if="!vIsOn" class="ui label tiny red basic">已关闭</span></div>'
|
||||
template: '<div><span v-if="vIsOn" class="ui label tiny green basic">已启用</span><span v-if="!vIsOn" class="ui label tiny red basic">已停用</span></div>'
|
||||
})
|
||||
71
web/views/@default/db/createPopup.html
Normal file
71
web/views/@default/db/createPopup.html
Normal file
@@ -0,0 +1,71 @@
|
||||
{$layout "layout_popup"}
|
||||
|
||||
<h3>添加节点</h3>
|
||||
|
||||
<form class="ui form" data-tea-action="$" data-tea-success="success">
|
||||
<table class="ui table definition selectable">
|
||||
<tr>
|
||||
<td class="title">节点名称 *</td>
|
||||
<td>
|
||||
<input type="text" name="name" maxlength="100" ref="focus"/>
|
||||
<p class="comment">给节点起一个容易识别的名称。</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>主机地址<em>(Host)</em> *</td>
|
||||
<td>
|
||||
<input type="text" name="host" placeholder="" maxlength="100"/>
|
||||
<p class="comment">连接数据库的主机地址,通常是域名或者IP。</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>数据库端口<em>(Port)</em> *</td>
|
||||
<td>
|
||||
<input type="text" name="port" value="3306" maxlength="5" style="width:6em"/>
|
||||
<p class="comment">连接数据库的端口。</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>数据库名称<em>(Database)</em> *</td>
|
||||
<td>
|
||||
<input type="text" name="database" value="" maxlength="100" placeholder="比如 edge_logs"/>
|
||||
<p class="comment">要写入日志的数据库名称,需要有创建新表和读写数据的权限。</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>用户名<em>(Username)</em> *</td>
|
||||
<td>
|
||||
<input type="text" name="username" value=""/>
|
||||
<p class="comment">连接数据库的用户名。</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>密码<em>(Password)</em></td>
|
||||
<td>
|
||||
<input type="password" name="password" value=""/>
|
||||
<p class="comment">连接数据库的密码。</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="2"><more-options-indicator></more-options-indicator></td>
|
||||
</tr>
|
||||
<tbody v-show="moreOptionsVisible">
|
||||
<tr>
|
||||
<td>详细描述</td>
|
||||
<td>
|
||||
<textarea rows="3" name="description"></textarea>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>是否启用</td>
|
||||
<td>
|
||||
<div class="ui checkbox">
|
||||
<input type="checkbox" name="isOn" value="1" checked="checked"/>
|
||||
<label></label>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<submit-btn></submit-btn>
|
||||
</form>
|
||||
3
web/views/@default/db/createPopup.js
Normal file
3
web/views/@default/db/createPopup.js
Normal file
@@ -0,0 +1,3 @@
|
||||
Tea.context(function () {
|
||||
this.success = NotifyPopup
|
||||
})
|
||||
@@ -1,3 +1,31 @@
|
||||
{$layout}
|
||||
|
||||
<p class="ui message">此功能暂未开放,敬请期待。</p>
|
||||
<first-menu>
|
||||
<a href="" class="item" @click.prevent="createNode()">[添加节点]</a>
|
||||
</first-menu>
|
||||
|
||||
<p class="comment" v-if="nodes.length == 0">暂时还没有数据库节点。</p>
|
||||
|
||||
<table class="ui table selectable" v-if="nodes.length > 0">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>节点名称</th>
|
||||
<th>连接地址</th>
|
||||
<th>数据库名</th>
|
||||
<th class="two wide">状态</th>
|
||||
<th class="two op">操作</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tr v-for="node in nodes">
|
||||
<td>{{node.name}}</td>
|
||||
<td>{{node.host}}:{{node.port}}</td>
|
||||
<td>{{node.database}}</td>
|
||||
<td><label-on :v-is-on="node.isOn"></label-on></td>
|
||||
<td>
|
||||
<a href="" @click.prevent="updateNode(node.id)">修改</a>
|
||||
<a href="" @click.prevent="deleteNode(node.id)">删除</a>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<div class="page" v-html="page"></div>
|
||||
37
web/views/@default/db/index.js
Normal file
37
web/views/@default/db/index.js
Normal file
@@ -0,0 +1,37 @@
|
||||
Tea.context(function () {
|
||||
// 添加节点
|
||||
this.createNode = function () {
|
||||
teaweb.popup("/db/createPopup", {
|
||||
height: "30em",
|
||||
callback: function () {
|
||||
teaweb.success("保存成功", function () {
|
||||
window.location.reload()
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 修改节点
|
||||
this.updateNode = function (nodeId) {
|
||||
teaweb.popup("/db/updatePopup?nodeId=" + nodeId, {
|
||||
height: "30em",
|
||||
callback: function () {
|
||||
teaweb.success("保存成功", function () {
|
||||
window.location.reload()
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 删除节点
|
||||
this.deleteNode = function (nodeId) {
|
||||
let that = this
|
||||
teaweb.confirm("确定要删除此数据库节点吗?", function () {
|
||||
that.$post(".delete")
|
||||
.params({
|
||||
nodeId: nodeId
|
||||
})
|
||||
.refresh()
|
||||
})
|
||||
}
|
||||
})
|
||||
72
web/views/@default/db/updatePopup.html
Normal file
72
web/views/@default/db/updatePopup.html
Normal file
@@ -0,0 +1,72 @@
|
||||
{$layout "layout_popup"}
|
||||
|
||||
<h3>修改节点</h3>
|
||||
|
||||
<form class="ui form" data-tea-action="$" data-tea-success="success">
|
||||
<input type="hidden" name="nodeId" :value="node.id"/>
|
||||
<table class="ui table definition selectable">
|
||||
<tr>
|
||||
<td class="title">节点名称 *</td>
|
||||
<td>
|
||||
<input type="text" name="name" maxlength="100" ref="focus" v-model="node.name"/>
|
||||
<p class="comment">给节点起一个容易识别的名称。</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>主机地址<em>(Host)</em> *</td>
|
||||
<td>
|
||||
<input type="text" name="host" placeholder="" maxlength="100" v-model="node.host"/>
|
||||
<p class="comment">连接数据库的主机地址,通常是域名或者IP。</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>数据库端口<em>(Port)</em> *</td>
|
||||
<td>
|
||||
<input type="text" name="port" value="" maxlength="5" style="width:6em" v-model="node.port"/>
|
||||
<p class="comment">连接数据库的端口。</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>数据库名称<em>(Database)</em> *</td>
|
||||
<td>
|
||||
<input type="text" name="database" value="" maxlength="100" placeholder="比如 edge_logs" v-model="node.database"/>
|
||||
<p class="comment">要写入日志的数据库名称,需要有创建新表和读写数据的权限。</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>用户名<em>(Username)</em> *</td>
|
||||
<td>
|
||||
<input type="text" name="username" value="" v-model="node.username"/>
|
||||
<p class="comment">连接数据库的用户名。</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>密码<em>(Password)</em></td>
|
||||
<td>
|
||||
<input type="password" name="password" value="" v-model="node.password"/>
|
||||
<p class="comment">连接数据库的密码。</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="2"><more-options-indicator></more-options-indicator></td>
|
||||
</tr>
|
||||
<tbody v-show="moreOptionsVisible">
|
||||
<tr>
|
||||
<td>详细描述</td>
|
||||
<td>
|
||||
<textarea rows="3" name="description" v-model="node.description"></textarea>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>是否启用</td>
|
||||
<td>
|
||||
<div class="ui checkbox">
|
||||
<input type="checkbox" name="isOn" value="1" v-model="node.isOn"/>
|
||||
<label></label>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<submit-btn></submit-btn>
|
||||
</form>
|
||||
3
web/views/@default/db/updatePopup.js
Normal file
3
web/views/@default/db/updatePopup.js
Normal file
@@ -0,0 +1,3 @@
|
||||
Tea.context(function () {
|
||||
this.success = NotifyPopup
|
||||
})
|
||||
Reference in New Issue
Block a user